<?php

if (! function_exists('snake2Title')) {
    /**
     * Convert a snake case string to a title with spaces
     * and every word capitalized.
     *
     * @param string $stakeSlug A snake case string, commonly a slug
     *
     * @return string
     */
    function snake2Title(string $snakeSlug): string
    {
        $output = preg_replace('/\_/', ' ', $snakeSlug);
        return ucwords($output);
    }
}

if (! function_exists('carbon')) {
    /**
     * Return a Carbon object based on a given timestring.
     * It will attempt to find a timezone in the current
     * session but default to UTC.
     *
     * @param string|null $timestring
     *
     * @return \Carbon\Carbon
     */
    function carbon(?string $timestring = null): \Carbon\Carbon
    {
        $carbon = Carbon\Carbon::now(session('timezone_name'));
        if (! empty($timestring)) {
            $carbon = Carbon\Carbon::parse($timestring, session('timezone_name'));
        }
        return $carbon;
    }
}

if (! function_exists('jddayofweek')) {
    /**
     * Returns the day of the week. Can return a string or an integer depending on the mode.
     *
     * @param int|null $intDay
     * @param int $mode
     *
     * @return string
     */
    function jddayofweek(?int $intDay = null, int $mode = 0): string
    {
        if (is_null($intDay)) {
            $intDay = date('l');
        }

        if ($mode === 0) {
            return $intDay;
        }

        return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][$intDay];
    }
}

if (! function_exists('cel2Fah')) {
    /**
     * Convert from celsius to fahrenheit.
     *
     * @param float|int|string $celsius
     * @param int $precision
     *
     * @return float
     */
    function cel2Fah($celsius, int $preceision = 0): float
    {
        return round((($celsius * (9/5)) + 32), $preceision);
    }
}

if (! function_exists('fah2Cel')) {
    /**
     * Convert from fahrenheit to celsius.
     *
     * @param float|int|string $fahrenheit
     * @param int $precision
     *
     * @return float
     */
    function fah2Cel($fahrenheit, int $preceision = 1): float
    {
        return round(($fahrenheit - 32 * (5/9)), $preceision);
    }
}

if (! function_exists('meters2Miles')) {
    /**
     * Convert from meters to miles.
     *
     * @param float|int|string $meters
     * @param int $precision
     *
     * @return float
     */
    function meters2Miles($meters, int $preceision = 1): float
    {
        return round(($meters * 0.00062137), $preceision);
    }
}

if (! function_exists('kilometers2Miles')) {
    /**
     * Convert from kilometers to meters.
     *
     * @param float|int|string $kilometers
     * @param int $precision
     *
     * @return float
     */
    function kilometers2Miles($kilometers, int $preceision = 1): float
    {
        return round(($kilometers * 1.609), $preceision);
    }
}

if (! function_exists('m2Km')) {
    /**
     * Convert from meters to kilometers.
     *
     * @param float|int|string $meters
     * @param int $precision
     *
     * @return float
     */
    function m2Km($meters, int $preceision = 1): float
    {
        return round(($meters / 1000), $preceision);
    }
}

if (! function_exists('mm2Inches')) {
    /**
     * Convert from milimeters to inches.
     *
     * @param float|int|string $milimeters
     * @param int $precision
     *
     * @return float
     */
    function mm2Inches($milimeters, int $preceision = 1): float
    {
        return round(($milimeters / 25.4), $preceision);
    }
}

if (! function_exists('pa2Mbar')) {
    /**
     * Convert from pascals to milibars.
     *
     * @param float|int|string $pascals
     * @param int $precision
     *
     * @return float
     */
    function pa2Mbar($pascals, int $preceision = 1): float
    {
        return round(($pascals / 100), $preceision);
    }
}
