import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__DataTemplateTitle_style_mjs__ from "./DataTemplateTitle.style.mjs";
const DataTemplateTitle = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _array_field;
    const array = __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.useArray();
    const index = __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.useIndex(props.index);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const value = null == array ? void 0 : null === (_array_field = array.field) || void 0 === _array_field ? void 0 : _array_field.value[index];
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!value.key) value.key = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)();
        if (!value.title) {
            var _array_field_value, _array_field;
            value.title = "".concat(t('Template name'), " ").concat(null == array ? void 0 : null === (_array_field = array.field) || void 0 === _array_field ? void 0 : null === (_array_field_value = _array_field.value) || void 0 === _array_field_value ? void 0 : _array_field_value.length);
        }
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Input.TextArea, {
        value: value.title,
        placeholder: t('Template name'),
        onChange: (ev)=>{
            ev.stopPropagation();
            value.title = ev.target.value;
        },
        onBlur: (ev)=>{
            ev.stopPropagation();
            value.title = ev.target.value;
        },
        autoSize: true,
        style: {
            width: '70%',
            border: 'none'
        },
        onClick: (e)=>{
            e.stopPropagation();
        }
    });
});
const isAdditionComponent = (schema)=>{
    var _schema_xcomponent_indexOf, _schema_xcomponent;
    return (null === (_schema_xcomponent = schema['x-component']) || void 0 === _schema_xcomponent ? void 0 : null === (_schema_xcomponent_indexOf = _schema_xcomponent.indexOf) || void 0 === _schema_xcomponent_indexOf ? void 0 : _schema_xcomponent_indexOf.call(_schema_xcomponent, 'Addition')) > -1;
};
const isIndexComponent = (schema)=>{
    var _schema_xcomponent_indexOf, _schema_xcomponent;
    return (null === (_schema_xcomponent = schema['x-component']) || void 0 === _schema_xcomponent ? void 0 : null === (_schema_xcomponent_indexOf = _schema_xcomponent.indexOf) || void 0 === _schema_xcomponent_indexOf ? void 0 : _schema_xcomponent_indexOf.call(_schema_xcomponent, 'Index')) > -1;
};
const isRemoveComponent = (schema)=>{
    var _schema_xcomponent_indexOf, _schema_xcomponent;
    return (null === (_schema_xcomponent = schema['x-component']) || void 0 === _schema_xcomponent ? void 0 : null === (_schema_xcomponent_indexOf = _schema_xcomponent.indexOf) || void 0 === _schema_xcomponent_indexOf ? void 0 : _schema_xcomponent_indexOf.call(_schema_xcomponent, 'Remove')) > -1;
};
const isMoveUpComponent = (schema)=>{
    var _schema_xcomponent_indexOf, _schema_xcomponent;
    return (null === (_schema_xcomponent = schema['x-component']) || void 0 === _schema_xcomponent ? void 0 : null === (_schema_xcomponent_indexOf = _schema_xcomponent.indexOf) || void 0 === _schema_xcomponent_indexOf ? void 0 : _schema_xcomponent_indexOf.call(_schema_xcomponent, 'MoveUp')) > -1;
};
const isMoveDownComponent = (schema)=>{
    var _schema_xcomponent_indexOf, _schema_xcomponent;
    return (null === (_schema_xcomponent = schema['x-component']) || void 0 === _schema_xcomponent ? void 0 : null === (_schema_xcomponent_indexOf = _schema_xcomponent.indexOf) || void 0 === _schema_xcomponent_indexOf ? void 0 : _schema_xcomponent_indexOf.call(_schema_xcomponent, 'MoveDown')) > -1;
};
const isCopyComponent = (schema)=>{
    var _schema_xcomponent_indexOf, _schema_xcomponent;
    return (null === (_schema_xcomponent = schema['x-component']) || void 0 === _schema_xcomponent ? void 0 : null === (_schema_xcomponent_indexOf = _schema_xcomponent.indexOf) || void 0 === _schema_xcomponent_indexOf ? void 0 : _schema_xcomponent_indexOf.call(_schema_xcomponent, 'Copy')) > -1;
};
const isOperationComponent = (schema)=>isAdditionComponent(schema) || isRemoveComponent(schema) || isMoveDownComponent(schema) || isMoveUpComponent(schema) || isCopyComponent(schema);
const range = (count)=>Array.from({
        length: count
    }).map((_, i)=>i);
const takeDefaultActiveKeys = (dataSourceLength, defaultOpenPanelCount)=>{
    if (dataSourceLength < defaultOpenPanelCount) return range(dataSourceLength);
    return range(defaultOpenPanelCount);
};
const insertActiveKeys = (activeKeys, index)=>{
    if (activeKeys.length <= index) return activeKeys.concat(index);
    return activeKeys.reduce((buf, key)=>{
        if (key < index) return buf.concat(key);
        if (key === index) return buf.concat([
            key,
            key + 1
        ]);
        return buf.concat(key + 1);
    }, []);
};
const ArrayCollapse = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__DataTemplateTitle_style_mjs__.useStyles)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const dataSource = Array.isArray(field.value) ? field.value : [];
    const [activeKeys, setActiveKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(takeDefaultActiveKeys(dataSource.length, props.defaultOpenPanelCount));
    const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!field.modified && dataSource.length) setActiveKeys(takeDefaultActiveKeys(dataSource.length, props.defaultOpenPanelCount));
    }, [
        dataSource.length,
        field
    ]);
    if (!schema) throw new Error('can not found schema object');
    const renderAddition = ()=>schema.reduceProperties((addition, schema, key)=>{
            if (isAdditionComponent(schema)) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                schema: schema,
                name: key
            });
            return addition;
        }, null);
    const renderEmpty = ()=>{
        if (dataSource.length) return;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Card, {
            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(styles.arrayCollapseItem), props.className),
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Empty, {})
        });
    };
    const renderItems = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Collapse, {
            ...props,
            activeKey: activeKeys,
            onChange: (keys)=>setActiveKeys((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.toArr)(keys).map(Number)),
            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(styles.arrayCollapseItem), props.className),
            children: dataSource.map((item, index)=>{
                const items = Array.isArray(schema.items) ? schema.items[index] || schema.items[0] : schema.items;
                const panelProps = field.query("".concat(field.address, ".").concat(index)).get('componentProps');
                const props = items['x-component-props'];
                const header = ()=>{
                    const header = "".concat((null == panelProps ? void 0 : panelProps.header) || props.header || field.title);
                    const path = field.address.concat(index);
                    const errors = field.form.queryFeedbacks({
                        type: 'error',
                        address: "".concat(path, ".**")
                    });
                    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.Item, {
                        index: index,
                        record: ()=>{
                            var _field_value;
                            return null === (_field_value = field.value) || void 0 === _field_value ? void 0 : _field_value[index];
                        },
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                                schema: items,
                                name: index,
                                filterProperties: (schema)=>{
                                    if (!isIndexComponent(schema)) return false;
                                    return true;
                                },
                                onlyRenderProperties: true
                            }),
                            errors.length ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Badge, {
                                size: "small",
                                className: "errors-badge",
                                count: errors.length,
                                children: header
                            }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DataTemplateTitle, {
                                item: item.initialValue || item,
                                index: index
                            })
                        ]
                    });
                };
                const extra = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.Item, {
                    index: index,
                    record: item,
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                            schema: items,
                            name: index,
                            filterProperties: (schema)=>{
                                if (!isOperationComponent(schema)) return false;
                                return true;
                            },
                            onlyRenderProperties: true
                        }),
                        null == panelProps ? void 0 : panelProps.extra
                    ]
                });
                const content = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                    schema: items,
                    name: index,
                    filterProperties: (schema)=>{
                        if (isIndexComponent(schema)) return false;
                        if (isOperationComponent(schema)) return false;
                        return true;
                    }
                });
                return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createElement)(__WEBPACK_EXTERNAL_MODULE_antd__.Collapse.Panel, {
                    ...props,
                    ...panelProps,
                    forceRender: true,
                    key: index,
                    header: header(),
                    extra: extra
                }, /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.Item, {
                    index: index,
                    record: item,
                    children: content
                }, index));
            })
        });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase, {
        onAdd: (index)=>{
            setActiveKeys(insertActiveKeys(activeKeys, index));
        },
        onRemove: ()=>{
            field.initialValue = field.value;
        },
        children: [
            renderEmpty(),
            renderItems(),
            renderAddition()
        ]
    });
}, {
    displayName: 'ArrayCollapse'
});
const CollapsePanel = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
        children: children
    });
};
CollapsePanel.displayName = 'CollapsePanel';
ArrayCollapse.defaultProps = {
    defaultOpenPanelCount: 5
};
ArrayCollapse.displayName = 'ArrayCollapse';
ArrayCollapse.CollapsePanel = CollapsePanel;
__WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.mixin(ArrayCollapse);
const DataTemplateTitle_rslib_entry_ = ArrayCollapse;
ArrayCollapse.Copy = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].forwardRef((props, ref)=>{
    var _array_field;
    const self = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const array = __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.useArray();
    const index = __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayBase.useIndex(props.index);
    if (!array) return null;
    if ((null === (_array_field = array.field) || void 0 === _array_field ? void 0 : _array_field.pattern) !== 'editable') return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CopyOutlined, {
        ...props,
        style: {
            transition: 'all 0.25s ease-in-out',
            color: 'rgba(0, 0, 0, 0.8)',
            fontSize: '16px',
            marginLeft: 6
        },
        ref: ref,
        onClick: (e)=>{
            var _array_props, _array_field;
            if (null == self ? void 0 : self.disabled) return;
            e.stopPropagation();
            if (null === (_array_props = array.props) || void 0 === _array_props ? void 0 : _array_props.disabled) return;
            const value = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.clone)(null == array ? void 0 : null === (_array_field = array.field) || void 0 === _array_field ? void 0 : _array_field.value[index]);
            array.field.push(value);
            if (props.onClick) props.onClick(e);
        }
    });
});
ArrayCollapse.Copy.displayName = 'ArrayCollapse.Copy';
export { ArrayCollapse, DataTemplateTitle_rslib_entry_ as default };
