"use strict";
/**
 * ConfigProvider — 主题对外唯一入口
 *
 * 支持嵌套：内层 Provider 的配置就近覆盖外层。
 * 通过 CSS 变量作用域实现局部主题，不污染外部。
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigProvider = void 0;
const react_1 = __importDefault(require("react"));
const context_1 = require("./context.js");
const merge_theme_1 = require("./merge-theme.js");
const resolve_theme_1 = require("./resolve-theme.js");
const brand_assets_1 = require("./brand-assets.js");
/**
 * 组件资产 URL 字段 → CSS 变量名的静态映射。
 * 只收录通过 CSS `background-image` / `mask` 消费的 URL 字段，
 * 保证业务仅通过 `theme.components` 配置即可生效。
 */
const ASSET_CSS_VAR_MAP = [
    { path: ['filter', 'recordMaskUrl'], varName: '--cnai-filter-record-mask-url' },
    { path: ['table', 'initPageBgUrl'], varName: '--cnai-table-illustration-init-page-url' },
    { path: ['table', 'illustrationGeneralUrl'], varName: '--cnai-table-illustration-general-url' },
    { path: ['table', 'illustrationDigitalUrl'], varName: '--cnai-table-illustration-digital-url' },
];
function normalizeUrlValue(raw) {
    const trimmed = raw.trim();
    if (trimmed.startsWith('url('))
        return trimmed;
    return `url('${trimmed}')`;
}
/**
 * 将品牌资产覆盖映射解析为可直接作为 inline style 使用的 CSS 变量键值对
 *
 * @param components - 品牌资产覆盖映射，undefined/null/'' 值会被跳过
 * @returns 已归一化的 CSS 变量键值对（key 带 `--` 前缀）
 */
function resolveAssetVars(components) {
    var _a;
    if (!components)
        return {};
    const result = {};
    for (const { path, varName } of ASSET_CSS_VAR_MAP) {
        const [comp, field] = path;
        const value = (_a = components[comp]) === null || _a === void 0 ? void 0 : _a[field];
        if (typeof value === 'string' && value) {
            result[varName] = normalizeUrlValue(value);
        }
    }
    return result;
}
/**
 * ConfigProvider 组件
 *
 * @param props - ConfigProvider 组件的 props
 * @param props.theme - 主题配置
 * @param props.children - 子组件
 * @returns ConfigProvider 组件
 */
const ConfigProvider = ({ theme, children }) => {
    const parentTheme = react_1.default.useContext(context_1.ThemeContext);
    const mergedTheme = react_1.default.useMemo(() => (0, merge_theme_1.mergeTheme)(parentTheme, theme), [parentTheme, theme]);
    const cssVars = react_1.default.useMemo(() => {
        const tokenVars = (0, resolve_theme_1.resolveTheme)(mergedTheme.token);
        const assetVars = resolveAssetVars(mergedTheme.components);
        return { ...tokenVars, ...assetVars };
    }, [mergedTheme.token, mergedTheme.components]);
    const assets = react_1.default.useMemo(() => (0, brand_assets_1.getAssets)(mergedTheme.components), [mergedTheme.components]);
    const hasVars = Object.keys(cssVars).length > 0;
    return (react_1.default.createElement(context_1.ThemeContext.Provider, { value: mergedTheme },
        react_1.default.createElement(context_1.AssetsContext.Provider, { value: assets }, hasVars ? (react_1.default.createElement("div", { style: { ...cssVars, display: 'contents' } }, children)) : (children))));
};
exports.ConfigProvider = ConfigProvider;
exports.ConfigProvider.displayName = 'ConfigProvider';
