<?php

use App\Models\Setting;

if (! function_exists('setting')) {
    /**
     * Get a setting value by key
     *
     * @param  string  $key
     * @param  mixed  $default
     * @param  string  $locale
     * @return mixed
     */
    function setting($key, $default = null, $locale = 'en')
    {
        return Setting::get($key, $default, $locale);
    }
}

if (! function_exists('public_asset_url')) {
    function public_asset_url($path): ?string
    {
        if (! $path) {
            return null;
        }

        if (! is_string($path)) {
            return null;
        }

        $path = trim($path);
        if ($path === '') {
            return null;
        }

        if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://') || str_starts_with($path, '//')) {
            return $path;
        }

        if (str_starts_with($path, '/storage/')) {
            return url($path);
        }

        if (str_starts_with($path, 'storage/')) {
            return url('/'.$path);
        }

        if (str_starts_with($path, '/')) {
            return url($path);
        }

        return asset('storage/'.$path);
    }
}
