"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFieldMapSetter = createFieldMapSetter;
const react_1 = __importDefault(require("react"));
const field_name_select_setter_1 = require("./field-name-select-setter.js");
/** 字段映射 setter 中需要隐藏的配置项 */
const ITEMS_TO_HIDE = ['rules', 'events', '__advancedCodeConfig__'];
/**
 * 创建字段映射（fieldMap）的 setter 配置
 * 基于低码 setterSnippet.createFormItemSetters，对 label/name 做自定义以支持从表格列选择
 */
function createFieldMapSetter() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    const createFormItemSetters = (_b = (_a = window === null || window === void 0 ? void 0 : window.cnLowCodeUtils) === null || _a === void 0 ? void 0 : _a.setterSnippet) === null || _b === void 0 ? void 0 : _b.createFormItemSetters;
    const DisplayPosition = (_e = (_d = (_c = window === null || window === void 0 ? void 0 : window.cnLowCodeUtils) === null || _c === void 0 ? void 0 : _c.enumMap) === null || _d === void 0 ? void 0 : _d.DisplayPosition) === null || _e === void 0 ? void 0 : _e.filter;
    if (!createFormItemSetters || !DisplayPosition)
        return undefined;
    const fullSetter = createFormItemSetters({ position: DisplayPosition });
    if (!((_j = (_h = (_g = (_f = fullSetter === null || fullSetter === void 0 ? void 0 : fullSetter.props) === null || _f === void 0 ? void 0 : _f.itemSetter) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.config) === null || _j === void 0 ? void 0 : _j.items))
        return fullSetter;
    // 过滤掉不需要的表单项（高级配置、分组等）
    const filteredItems = fullSetter.props.itemSetter.props.config.items.filter((item) => {
        if (ITEMS_TO_HIDE.includes(item.name))
            return false;
        if (item.type === 'group' && item.items)
            return false;
        return true;
    });
    // 自定义 label：用 FieldNameSelectSetter 从表格列中选择；name：只读，由 label 联动
    const mappedItems = filteredItems.map((item) => {
        if (item.name === 'label') {
            return {
                ...item,
                setter: {
                    componentName: react_1.default.createElement(field_name_select_setter_1.FieldNameSelectSetter),
                },
            };
        }
        if (item.name === 'name') {
            return {
                ...item,
                editable: false,
                setter: { componentName: 'StringSetter', props: { disabled: true } },
            };
        }
        return item;
    });
    const origInitialValue = fullSetter.props.itemSetter.initialValue;
    return {
        ...fullSetter,
        props: {
            ...fullSetter.props,
            itemSetter: {
                ...fullSetter.props.itemSetter,
                initialValue(prop) {
                    const base = typeof origInitialValue === 'function' ? origInitialValue(prop) : {};
                    // 初始不预填 label/name，由用户从表格列中选择
                    return { ...base, label: undefined, name: undefined };
                },
                props: {
                    ...fullSetter.props.itemSetter.props,
                    config: {
                        ...fullSetter.props.itemSetter.props.config,
                        items: mappedItems,
                    },
                },
            },
        },
    };
}
