<?php
use App\Models\GenericBackoffice\WebsiteObjectLanguage;

if (!function_exists('path_asset')) {
    function path_asset($configKey)
    {
        return asset(config($configKey));
    }
}

if (!function_exists('path_public_path')) {
    function path_public_path($configKey)
    {
        return public_path(config($configKey));
    }
}


if (!function_exists('getLinkPath')) {
    function getLinkPath($link)
    {
        if (empty($link)) {
            return "";
        }
        if (is_string($link)) {
            $link = json_decode($link);
        }

        if (is_array($link)) {
            $link = (object) $link;
        }

        $url = "";

        if ($link->isExternal && !$link->url || !$link->isExternal && !$link->objectId) {
            return "";
        } else {
            $url = $link->url;
        }

        if ($link->isExternal) {
            return $link;
        } else {
            $uri = WebsiteObjectLanguage::select('lang_uri')
                ->where('website_object_id', $link->objectId)
                ->where('wo_language_id', getActiveLanguage())
                ->first();
            if (empty($uri)) {
                return "";
            }
            $url = $uri->lang_uri;
        }

        $link->path = $url;

        return $link;
    }
}
