<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Support;

/**
 * 业务字典项维护策略：按字典类型编码区分「系统固定」与「业务可维护」。
 *
 * 未在配置中显式列入「业务可维护」的类型，默认按系统固定处理，避免插件新增类型被误放开权限。
 */
final class DberpDictionaryItemMaintenance
{
    public const STRATEGY_SYSTEM_FIXED = 'system_fixed';

    public const STRATEGY_BUSINESS_MAINTAINABLE = 'business_maintainable';

    /**
     * @return value-of<self::STRATEGY_*>
     */
    public static function strategyForTypeCode(?string $typeCode): string
    {
        if ($typeCode === null || $typeCode === '') {
            return self::STRATEGY_SYSTEM_FIXED;
        }

        $business = self::businessMaintainableTypeCodes();

        if (in_array($typeCode, $business, true)) {
            return self::STRATEGY_BUSINESS_MAINTAINABLE;
        }

        $system = self::systemFixedTypeCodes();

        if (in_array($typeCode, $system, true)) {
            return self::STRATEGY_SYSTEM_FIXED;
        }

        return self::STRATEGY_SYSTEM_FIXED;
    }

    public static function isSystemFixedForTypeCode(?string $typeCode): bool
    {
        return self::strategyForTypeCode($typeCode) === self::STRATEGY_SYSTEM_FIXED;
    }

    public static function isBusinessMaintainableForTypeCode(?string $typeCode): bool
    {
        return self::strategyForTypeCode($typeCode) === self::STRATEGY_BUSINESS_MAINTAINABLE;
    }

    /**
     * @return list<string>
     */
    private static function systemFixedTypeCodes(): array
    {
        $configured = config('dberp.dictionary.type_item_maintenance.system_fixed_type_codes');

        return is_array($configured) ? array_values(array_filter(array_map('strval', $configured))) : [];
    }

    /**
     * @return list<string>
     */
    private static function businessMaintainableTypeCodes(): array
    {
        $configured = config('dberp.dictionary.type_item_maintenance.business_maintainable_type_codes');

        return is_array($configured) ? array_values(array_filter(array_map('strval', $configured))) : [];
    }
}
