<?php 

namespace App\Services\Views;

//网站配置信息
use App\Models\Setting;

class SettingService
{
    use \App\Traits\ContentTrait;

    //获取系统配置
    public function config($item)
    {
        static $data = null;

        $data = $data ?? self::model('setting')::where('lang', 'single')->orWhere('lang', config('lang'))->lists('value', 'name');

        if ($item == 'chat_code' && isset($data[$item]) && $data[$item] && isset(config('setting.meiqiaLang')[config('lang')])) {
            $lang = config('setting.meiqiaLang')[config('lang')];
            $data[$item]  = preg_replace("/_MEIQIA\('language',\s*'([a-zA-Z]+)'\);/", "_MEIQIA('language', '{$lang}');", $data[$item]);
        }

        if ($item == 'logoes') {
            $logo = $data['logoes'] ?? '';

            if (!$logo) {
                $result = self::model('setting')::where('lang', config('setting.lang'))->where('name', 'logoes')->lists('value', 'name');
                $logo = $result['logoes'];
            }

            if (Setting::get('is_webp')) {
                $logo =  str_contains($logo, '?x-oss-process=image/format,webp') ? $logo : $logo . '?x-oss-process=image/format,webp';
            }

            return $logo;
        }

        if ($item == 'message_email') {
            $emails = json_decode($data['message_email'], true);

            return $emails[0] ?? '';
        }

        if (array_key_exists($item, config('system.brand'))) {
            return isset($data[$item]) ? ($data[$item] ?: config('system.brand')[$item]) : config('system.brand')[$item];
        }
        
        return $data[$item] ?? '';
    }

    //获取视图语言
    public function langs($fileName = 'common', $field = '') 
    {
        static $data = null;

        $data[$fileName] = $data[$fileName] ?? trans("language::{$fileName}");

        return $data[$fileName][$field] ?? '';
    }

    //获取商城视图语言
    public function translate($fileName = 'common', $field = '')
    {
        static $data = null;

        $data[$fileName] = $data[$fileName] ?? trans("translate::{$fileName}");

        return $data[$fileName][$field] ?? '';
    }

}