"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    default: ()=>DataTemplateTitle_rslib_entry_,
    ArrayCollapse: ()=>ArrayCollapse
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_classnames_namespaceObject = require("classnames");
var external_classnames_default = /*#__PURE__*/ __webpack_require__.n(external_classnames_namespaceObject);
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_DataTemplateTitle_style_js_namespaceObject = require("./DataTemplateTitle.style.js");
const DataTemplateTitle = (0, schema_namespaceObject.observer)((props)=>{
    var _array_field;
    const array = components_namespaceObject.ArrayBase.useArray();
    const index = components_namespaceObject.ArrayBase.useIndex(props.index);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const value = null == array ? void 0 : null === (_array_field = array.field) || void 0 === _array_field ? void 0 : _array_field.value[index];
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!value.key) value.key = (0, schema_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, schema_namespaceObject.observer)((props)=>{
    const { styles } = (0, external_DataTemplateTitle_style_js_namespaceObject.useStyles)();
    const field = (0, schema_namespaceObject.useField)();
    const dataSource = Array.isArray(field.value) ? field.value : [];
    const [activeKeys, setActiveKeys] = (0, external_react_namespaceObject.useState)(takeDefaultActiveKeys(dataSource.length, props.defaultOpenPanelCount));
    const schema = (0, schema_namespaceObject.useFieldSchema)();
    (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                schema: schema,
                name: key
            });
            return addition;
        }, null);
    const renderEmpty = ()=>{
        if (dataSource.length) return;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Card, {
            className: external_classnames_default()("".concat(styles.arrayCollapseItem), props.className),
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Empty, {})
        });
    };
    const renderItems = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Collapse, {
            ...props,
            activeKey: activeKeys,
            onChange: (keys)=>setActiveKeys((0, schema_namespaceObject.toArr)(keys).map(Number)),
            className: external_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, jsx_runtime_namespaceObject.jsxs)(components_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                                schema: items,
                                name: index,
                                filterProperties: (schema)=>{
                                    if (!isIndexComponent(schema)) return false;
                                    return true;
                                },
                                onlyRenderProperties: true
                            }),
                            errors.length ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Badge, {
                                size: "small",
                                className: "errors-badge",
                                count: errors.length,
                                children: header
                            }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DataTemplateTitle, {
                                item: item.initialValue || item,
                                index: index
                            })
                        ]
                    });
                };
                const extra = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(components_namespaceObject.ArrayBase.Item, {
                    index: index,
                    record: item,
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                    schema: items,
                    name: index,
                    filterProperties: (schema)=>{
                        if (isIndexComponent(schema)) return false;
                        if (isOperationComponent(schema)) return false;
                        return true;
                    }
                });
                return /*#__PURE__*/ (0, external_react_namespaceObject.createElement)(external_antd_namespaceObject.Collapse.Panel, {
                    ...props,
                    ...panelProps,
                    forceRender: true,
                    key: index,
                    header: header(),
                    extra: extra
                }, /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.ArrayBase.Item, {
                    index: index,
                    record: item,
                    children: content
                }, index));
            })
        });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(components_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_react_namespaceObject.Fragment, {
        children: children
    });
};
CollapsePanel.displayName = 'CollapsePanel';
ArrayCollapse.defaultProps = {
    defaultOpenPanelCount: 5
};
ArrayCollapse.displayName = 'ArrayCollapse';
ArrayCollapse.CollapsePanel = CollapsePanel;
components_namespaceObject.ArrayBase.mixin(ArrayCollapse);
const DataTemplateTitle_rslib_entry_ = ArrayCollapse;
ArrayCollapse.Copy = /*#__PURE__*/ external_react_default().forwardRef((props, ref)=>{
    var _array_field;
    const self = (0, schema_namespaceObject.useField)();
    const array = components_namespaceObject.ArrayBase.useArray();
    const index = components_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.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, external_lodash_namespaceObject.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';
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
