"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldSelector = FieldSelector;
const i18n_1 = __importDefault(require("../../../../../locales/i18n.js"));
const React = __importStar(require("react"));
const cn_ui_1 = require("@cainiaofe/cn-ui");
const use_ai_table_icons_1 = require("../../../../../cn-ai-table/view/hooks/use-ai-table-icons");
require("./field-selector.scss");
const field_map_1 = require("./field-map");
const lookup_field_icon_1 = require("../../../../../cn-ai-table/view/constants/lookup-field-icon");
const CustomIcon = cn_ui_1.CnIcon.createFromIconfontCN({
    scriptUrl: '//at.alicdn.com/t/a/font_4448706_i4lwtal07u.js',
});
const CATEGORY_LABELS = {
    ai: i18n_1.default.get({ id: 'AIField', dm: 'AI字段' }),
    agent: i18n_1.default.get({ id: 'CaiBotAIField', dm: '菜包AI字段' }),
    regular: i18n_1.default.get({ id: 'GeneralFields', dm: '常规字段' }),
    business: i18n_1.default.get({ id: 'BusinessField', dm: '业务字段' }),
    custom: i18n_1.default.get({ id: 'GeneralFields', dm: '常规字段' }),
};
function FieldSelector({ onVisibleChange, onSelect, enableAiFields = true, items, }) {
    const icons = (0, use_ai_table_icons_1.useAiTableIcons)();
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const [searchValue, setSearchValue] = React.useState('');
    const handleFieldClick = (fieldType) => {
        onSelect === null || onSelect === void 0 ? void 0 : onSelect(fieldType);
        onVisibleChange === null || onVisibleChange === void 0 ? void 0 : onVisibleChange(false);
    };
    const filterBySearch = (fields) => {
        if (!searchValue)
            return fields;
        return fields.filter((field) => { var _a; return (_a = field.label) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(searchValue === null || searchValue === void 0 ? void 0 : searchValue.toLowerCase()); });
    };
    const getIconContent = (field) => {
        const { icon, iconType } = field;
        if (iconType === 'ai') {
            return (React.createElement("img", { src: icons.FIELD_AI, alt: i18n_1.default.get({ id: 'AIField', dm: 'AI字段' }), style: { width: '16px', height: '16px' } }));
        }
        else if (iconType === 'agent') {
            return (React.createElement("img", { src: icons.FIELD_AGENT, alt: i18n_1.default.get({ id: 'AgentField', dm: 'Agent字段' }), style: { width: '16px', height: '16px' } }));
        }
        else if (iconType === 'business') {
            return (React.createElement("img", { src: icons.FIELD_BUSINESS, alt: i18n_1.default.get({ id: 'BusinessField', dm: '业务字段' }), style: { width: '16px', height: '16px' } }));
        }
        else if (iconType === 'custom-iconfont') {
            return React.createElement(CustomIcon, { type: `icon-${icon}`, style: { fontSize: '16px', color: '#666' } });
        }
        else if (iconType === 'reference-field') {
            return (React.createElement("img", { src: lookup_field_icon_1.LOOKUP_FIELD_ICON_URL, alt: "", style: { width: '16px', height: '16px', display: 'block' }, width: 16, height: 16 }));
        }
        else if (iconType === 'regular') {
            return React.createElement(cn_ui_1.CnIcon, { type: "list", style: { fontSize: '16px', color: '#666' } });
        }
        const iconTypeMap = {
            magic: 'magic',
            number: 'number',
            tag: 'tag',
            text: 'text',
            calendar: 'calendar',
            dollar: 'dollar',
            progress: 'progress',
            formula: 'formula',
            select: 'select',
            list: 'list',
            user: 'user',
            attachment: 'attachment',
            link: 'link',
            button: 'button',
            checkbox: 'checkbox',
            phone: 'phone',
            star: 'star',
            workflow: 'workflow',
            team: 'team',
            group: 'group',
            edit: 'edit',
            file: 'file',
            add: 'add',
            search: 'search',
        };
        const iconTypeName = iconTypeMap[icon] || icon;
        return React.createElement(cn_ui_1.CnIcon, { type: iconTypeName, style: { fontSize: '16px', color: '#666' } });
    };
    // ---- registry-driven rendering ----
    if (items) {
        const aiItems = filterBySearch(items.filter((i) => i.category === 'ai' || i.category === 'agent'));
        const regularItems = filterBySearch(items.filter((i) => i.category === 'regular' || i.category === 'custom'));
        const renderItem = (item) => (React.createElement("div", { key: item.type, className: "field-selector-item", onClick: () => handleFieldClick(item.type) },
            React.createElement("span", { className: "field-selector-item-icon", style: { color: item.color || '#666' } }, getIconContent(item)),
            React.createElement("span", null, item.label)));
        return (React.createElement("div", { className: "field-selector" },
            React.createElement("div", { className: "field-selector-content" },
                enableAiFields && aiItems.length > 0 && (React.createElement("div", { className: "field-selector-section" },
                    React.createElement("div", { className: "field-selector-section-title" }, CATEGORY_LABELS.ai),
                    React.createElement("div", { className: "field-selector-section-grid" }, aiItems.map(renderItem)))),
                regularItems.length > 0 && (React.createElement("div", { className: "field-selector-section" },
                    React.createElement("div", { className: "field-selector-section-title" }, CATEGORY_LABELS.regular),
                    React.createElement("div", { className: "field-selector-section-grid" }, regularItems.map(renderItem)))))));
    }
    // ---- legacy fallback (field-map.ts 硬编码) ----
    const filterFields = (fields) => filterBySearch(fields);
    const renderFieldItem = (field) => (React.createElement("div", { key: field.type, className: "field-selector-item", onClick: () => handleFieldClick(field.type) },
        React.createElement("span", { className: "field-selector-item-icon", style: { color: field.color || '#666' } }, getIconContent(field)),
        React.createElement("span", null, field.label)));
    return (React.createElement("div", { className: "field-selector" },
        React.createElement("div", { className: "field-selector-content" },
            enableAiFields && (React.createElement("div", { className: "field-selector-section" },
                React.createElement("div", { className: "field-selector-section-title" }, i18n_1.default.get({ id: 'CaiBotAIField', dm: '菜包AI字段' })),
                React.createElement("div", { className: "field-selector-section-grid" }, filterFields(field_map_1.aiFields).map(renderFieldItem)))),
            React.createElement("div", { className: "field-selector-section" },
                React.createElement("div", { className: "field-selector-section-title" }, i18n_1.default.get({ id: 'GeneralFields', dm: '常规字段' })),
                React.createElement("div", { className: "field-selector-section-grid" }, filterFields(field_map_1.regularFields).map(renderFieldItem))))));
}
