<?php

if (!function_exists('cmsTemplateCategoryOptions')) {
    function cmsTemplateCategoryOptions()
    {
        return [
            'it' => 'IT/软件/信息/互联网',
            'tech' => '科技/电子/数码/通信',
            'culture' => '文化/艺术/广告/传媒',
            'design' => '装修/设计/家居/家具',
            'estate' => '基建/施工/地产/物业',
            'catering' => '餐饮/酒店/旅游/票务',
            'food' => '食品/果蔬/饮料/日用',
            'fashion' => '服饰/珠宝/礼品/玩具',
            'life' => '摄影/婚庆/家政/生活',
            'sports' => '运动/健身/体育/器材',
            'education' => '学校/教育/培训/科研',
            'medical' => '美容/保健/医院/医疗',
            'finance' => '金融/财税/咨询/法律',
            'government' => '政府/组织/集团/协会',
            'auto' => '汽车/物流/交通/搬运',
            'machinery' => '机械/设备/制造/仪器',
            'chemical' => '化工/环保/能源/材料',
            'agriculture' => '农业/畜牧/养殖/宠物',
            'trade' => '外贸',
            'mobile' => '独立手机版',
            'blog' => '新闻博客',
            'single' => '单页面',
            'marketing' => '营销型模板',
            'other' => '其他模板',
        ];
    }
}

if (!function_exists('cmsTemplateLegacyCategoryMap')) {
    function cmsTemplateLegacyCategoryMap()
    {
        return [
            'default' => 'other',
            'free' => 'other',
            'basic' => 'other',
            'pro' => 'other',
        ];
    }
}

if (!function_exists('cmsTemplateNormalizeCategoryKey')) {
    function cmsTemplateNormalizeCategoryKey($category)
    {
        $category = trim((string) $category);
        if ($category === '') {
            return 'other';
        }

        $options = cmsTemplateCategoryOptions();
        if (isset($options[$category])) {
            return $category;
        }

        $legacyMap = cmsTemplateLegacyCategoryMap();
        if (isset($legacyMap[$category])) {
            return $legacyMap[$category];
        }

        return 'other';
    }
}

if (!function_exists('cmsTemplateCategoryLabel')) {
    function cmsTemplateCategoryLabel($category)
    {
        $category = trim((string) $category);
        $options = cmsTemplateCategoryOptions();
        if (isset($options[$category])) {
            return $options[$category];
        }

        $legacyMap = cmsTemplateLegacyCategoryMap();
        if (isset($legacyMap[$category]) && isset($options[$legacyMap[$category]])) {
            return $options[$legacyMap[$category]];
        }

        return $category !== '' ? $category : $options['other'];
    }
}

if (!function_exists('cmsTemplateCreatedTimestamp')) {
    function cmsTemplateCreatedTimestamp(array $template)
    {
        foreach (['created_at', 'create_time', 'published_at'] as $field) {
            $value = $template[$field] ?? null;
            if (is_numeric($value) && intval($value) > 0) {
                return intval($value);
            }
            if (is_string($value) && trim($value) !== '') {
                $timestamp = strtotime($value);
                if ($timestamp !== false) {
                    return intval($timestamp);
                }
            }
        }
        return 0;
    }
}

if (!function_exists('cmsSortTemplatesNewestFirst')) {
    function cmsSortTemplatesNewestFirst(array $templates)
    {
        $templates = array_values($templates);
        usort($templates, function ($left, $right) {
            $leftTime = cmsTemplateCreatedTimestamp($left);
            $rightTime = cmsTemplateCreatedTimestamp($right);
            if ($leftTime !== $rightTime) {
                return $rightTime <=> $leftTime;
            }
            $leftId = intval($left['id'] ?? 0);
            $rightId = intval($right['id'] ?? 0);
            if ($leftId !== $rightId) {
                return $rightId <=> $leftId;
            }
            return strcasecmp((string) ($right['template_key'] ?? ''), (string) ($left['template_key'] ?? ''));
        });
        return $templates;
    }
}
