"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
// 支持的输入类型集合
const SUPPORTED_INPUT_TYPES = new Set(['input', 'select', 'custom', 'content']);
/**
 * 从 slot 配置中提取默认值构建 slotValues
 */
const buildSlotValues = (slotConfig) => {
    var _a;
    return (_a = slotConfig === null || slotConfig === void 0 ? void 0 : slotConfig.reduce((acc, node) => {
        var _a;
        const { key, type } = node;
        if (!key)
            return acc;
        const props = node.props || {};
        // 优先读取 props.value（用于常用查询条件等场景的 initialValue）
        // 其次读取 props.defaultValue（用于 input/select 等场景）
        // 最后回退到 props.label
        const defaultValue = (_a = props.value) !== null && _a !== void 0 ? _a : (SUPPORTED_INPUT_TYPES.has(type) ? props.defaultValue : props.label);
        acc[key] = defaultValue !== null && defaultValue !== void 0 ? defaultValue : '';
        return acc;
    }, {})) !== null && _a !== void 0 ? _a : {};
};
/**
 * 将 slot 配置数组转换为 Map 结构便于查找
 */
const buildSlotConfigMap = (slotConfig, slotConfigMap) => {
    slotConfig === null || slotConfig === void 0 ? void 0 : slotConfig.forEach((node) => {
        if (node.key)
            slotConfigMap.set(node.key, node);
    });
};
/**
 * 根据目标节点和配置映射获取节点信息
 */
const getNodeInfoBySlotConfigMap = (targetNode, slotConfigMap) => {
    if (!(targetNode === null || targetNode === void 0 ? void 0 : targetNode.dataset))
        return null;
    const { dataset } = targetNode;
    return {
        slotKey: dataset.slotKey,
        placeholder: dataset.placeholder,
        nodeType: dataset.nodeType,
        skillKey: dataset.skillKey,
        slotConfig: dataset.slotKey ? slotConfigMap.get(dataset.slotKey) : undefined,
        targetNode,
    };
};
const useSlotConfigState = (slotConfig, formMapRef) => {
    const [state, _setState] = (0, react_1.useState)({});
    const stateRef = (0, react_1.useRef)(state);
    const slotConfigMapRef = (0, react_1.useRef)(new Map());
    // 初始化 slotConfig
    (0, react_1.useEffect)(() => {
        if (!slotConfig)
            return;
        slotConfigMapRef.current.clear();
        buildSlotConfigMap(slotConfig, slotConfigMapRef.current);
        const newValues = buildSlotValues(slotConfig);
        _setState(newValues);
        stateRef.current = newValues;
    }, [slotConfig]);
    const setState = (0, react_1.useCallback)((newValue) => {
        const value = typeof newValue === 'function' ? newValue(stateRef.current) : newValue;
        _setState(value);
        stateRef.current = value;
    }, []);
    const mergeSlotConfig = (0, react_1.useCallback)((newSlotConfig) => {
        const newValues = buildSlotValues(newSlotConfig);
        // 更新配置映射
        newSlotConfig.forEach((node) => {
            if (node.key)
                slotConfigMapRef.current.set(node.key, node);
        });
        _setState((prev) => ({ ...prev, ...newValues }));
        stateRef.current = { ...stateRef.current, ...newValues };
    }, []);
    const getNodeInfo = (0, react_1.useCallback)((targetNode) => getNodeInfoBySlotConfigMap(targetNode, slotConfigMapRef.current), []);
    const getNodeTextValue = (node) => {
        var _a, _b, _c;
        const { nodeType } = node;
        if (nodeType === Node.TEXT_NODE) {
            return node.textContent || '';
        }
        if (nodeType !== Node.ELEMENT_NODE) {
            return '';
        }
        const element = node;
        const nodeInfo = getNodeInfo(element);
        // 无节点信息，直接返回文本内容
        if (!nodeInfo) {
            return element.innerText || '';
        }
        const { slotKey, skillKey, nodeType: infoNodeType, slotConfig } = nodeInfo;
        if (skillKey) {
            return '';
        }
        // 缓存文本内容，避免重复获取
        const textContent = element.innerText || '';
        // 处理 slot 节点
        if (slotKey) {
            if (infoNodeType === 'nbsp') {
                return ' ';
            }
            if (!slotConfig || slotConfig.type === 'content') {
                return textContent;
            }
            const slotValue = (_a = stateRef.current[slotKey]) !== null && _a !== void 0 ? _a : '';
            return (_c = (_b = slotConfig.formatResult) === null || _b === void 0 ? void 0 : _b.call(slotConfig, slotValue, slotConfig, formMapRef)) !== null && _c !== void 0 ? _c : slotValue;
        }
        return textContent;
    };
    return [
        slotConfigMapRef.current,
        {
            getSlotValues: () => stateRef.current,
            setSlotValues: setState,
            mergeSlotConfig,
            getNodeInfo,
            getNodeTextValue,
        },
    ];
};
exports.default = useSlotConfigState;
