"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__, {
    useAclCheck: ()=>useAclCheck,
    CreateAction: ()=>CreateAction,
    CreateRecordAction: ()=>CreateRecordAction
});
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 schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const ref_namespaceObject = require("rc-util/lib/ref");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../collection-manager/index.js");
const external_record_provider_index_js_namespaceObject = require("../../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const utils_js_namespaceObject = require("../../schema-component/antd/action/utils.js");
const uitls_js_namespaceObject = require("../../schema-component/common/utils/uitls.js");
const external_variables_index_js_namespaceObject = require("../../variables/index.js");
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      position: relative;\n      &:hover {\n        .general-schema-designer {\n          display: block;\n        }\n      }\n      .general-schema-designer {\n        position: absolute;\n        z-index: 999;\n        top: 0;\n        bottom: 0;\n        left: 0;\n        right: 0;\n        display: none;\n        background: var(--colorBgSettingsHover);\n        border: 0;\n        pointer-events: none;\n        > .general-schema-designer-icons {\n          position: absolute;\n          right: 2px;\n          top: 2px;\n          line-height: 16px;\n          pointer-events: all;\n          .ant-space-item {\n            background-color: var(--colorSettings);\n            color: #fff;\n            line-height: 16px;\n            width: 16px;\n            padding-left: 1px;\n            align-self: stretch;\n          }\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      position: relative;\n      &:hover {\n        .general-schema-designer {\n          display: block;\n        }\n      }\n      .general-schema-designer {\n        position: absolute;\n        z-index: 999;\n        top: 0;\n        bottom: 0;\n        left: 0;\n        right: 0;\n        display: none;\n        background: var(--colorBgSettingsHover);\n        border: 0;\n        top: 0;\n        bottom: 0;\n        left: 0;\n        right: 0;\n        pointer-events: none;\n        > .general-schema-designer-icons {\n          position: absolute;\n          right: 2px;\n          top: 2px;\n          line-height: 16px;\n          pointer-events: all;\n          .ant-space-item {\n            background-color: var(--colorSettings);\n            color: #fff;\n            line-height: 16px;\n            width: 16px;\n            padding-left: 1px;\n            align-self: stretch;\n          }\n        }\n      }\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        designer: css(_templateObject()),
        actionDesigner: css(_templateObject1())
    };
});
function useAclCheck(actionPath) {
    const aclCheck = useAclCheckFn();
    return aclCheck(actionPath);
}
function useAclCheckFn() {
    const { data, inResources, getResourceActionParams, getStrategyActionParams } = (0, external_index_js_namespaceObject.useACLRolesCheck)();
    const recordPkValue = (0, external_index_js_namespaceObject.useRecordPkValue)();
    const collection = (0, index_js_namespaceObject.useCollection_deprecated)();
    function actionAclCheck(actionPath) {
        const resource = collection.resource;
        const parseAction = function(actionPath) {
            arguments.length > 1 && void 0 !== arguments[1] && arguments[1];
            const [resourceName] = actionPath.split(':');
            if (null == data ? void 0 : data.allowAll) return {};
            if (inResources(resourceName)) return getResourceActionParams(actionPath);
            return getStrategyActionParams(actionPath);
        };
        if (!actionPath && resource) actionPath = "".concat(resource, ":create}");
        if (!(null == actionPath ? void 0 : actionPath.includes(':'))) actionPath = "".concat(resource, ":").concat(actionPath);
        if (!actionPath) return true;
        const params = parseAction(actionPath, {
            recordPkValue
        });
        if (!params) return false;
        return true;
    }
    return actionAclCheck;
}
const InternalCreateRecordAction = (props, ref)=>{
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const collection = (0, index_js_namespaceObject.useCollection_deprecated)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const field = (0, schema_namespaceObject.useField)();
    const [currentCollection, setCurrentCollection] = (0, external_react_namespaceObject.useState)(collection.name);
    const [currentCollectionDataSource, setCurrentCollectionDataSource] = (0, external_react_namespaceObject.useState)(collection.dataSource);
    const linkageRules = (null == fieldSchema ? void 0 : fieldSchema['x-linkage-rules']) || [];
    const values = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const ctx = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const variables = (0, external_variables_index_js_namespaceObject.useVariables)();
    const { styles } = useStyles();
    const localVariables = (0, external_variables_index_js_namespaceObject.useLocalVariables)({
        currentForm: {
            values
        }
    });
    (0, external_react_namespaceObject.useEffect)(()=>{
        field.stateOfLinkageRules = {};
        linkageRules.filter((k)=>!k.disabled).forEach((v)=>{
            var _v_actions;
            null === (_v_actions = v.actions) || void 0 === _v_actions || _v_actions.forEach((h)=>{
                (0, utils_js_namespaceObject.linkageAction)({
                    operator: h.operator,
                    field,
                    condition: v.condition,
                    variables,
                    localVariables
                });
            });
        });
    }, [
        field,
        linkageRules,
        localVariables,
        variables
    ]);
    const internalRef = /*#__PURE__*/ (0, external_react_namespaceObject.createRef)();
    const buttonRef = (0, ref_namespaceObject.composeRef)(ref, internalRef);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: styles.actionDesignerCss,
        ref: buttonRef,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_schema_component_index_js_namespaceObject.ActionContextProvider, {
            value: {
                ...ctx,
                visible,
                setVisible
            },
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CreateAction, {
                    ...props,
                    onClick: (collectionData)=>{
                        setVisible(true);
                        setCurrentCollection(collectionData.name);
                        setCurrentCollectionDataSource(collectionData.dataSource);
                    }
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.CollectionProvider_deprecated, {
                    name: currentCollection,
                    dataSource: currentCollectionDataSource,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                        schema: fieldSchema,
                        basePath: field.address,
                        onlyRenderProperties: true
                    })
                })
            ]
        })
    });
};
function getLinkageCollection(str, form, field) {
    const variablesCtx = {
        $form: form.values,
        $iteration: form.values
    };
    if (str.includes('$iteration')) {
        const path = field.path.segments.concat([]);
        path.splice(-2);
        str = str.replace('$iteration.', "$iteration.".concat(path.join('.'), "."));
        const data = (0, uitls_js_namespaceObject.parseVariables)(str, variablesCtx);
        return data;
    }
    {
        const data = (0, uitls_js_namespaceObject.parseVariables)(str, {
            $form: form.values
        });
        return data;
    }
}
const CreateAction = (0, schema_namespaceObject.observer)((props)=>{
    var _fieldSchema_xcomponentprops;
    const { onClick } = props;
    const collection = (0, index_js_namespaceObject.useCollection_deprecated)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, schema_namespaceObject.useForm)();
    const variables = (0, external_variables_index_js_namespaceObject.useVariables)();
    const aclCheck = useAclCheckFn();
    const enableChildren = fieldSchema['x-enable-children'] || [];
    const allowAddToCurrent = null == fieldSchema ? void 0 : fieldSchema['x-allow-add-to-current'];
    const linkageFromForm = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['linkageFromForm'];
    const componentType = 'danger' === field.componentProps.type ? void 0 : field.componentProps.type || 'primary';
    const { getChildrenCollections } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const totalChildCollections = getChildrenCollections(collection.name);
    const inheritsCollections = (0, external_react_namespaceObject.useMemo)(()=>enableChildren.map((k)=>{
            if (!k) return;
            const childCollection = totalChildCollections.find((j)=>j.name === k.collection);
            if (!childCollection) return;
            return {
                ...childCollection.getOptions(),
                title: k.title || childCollection.title
            };
        }).filter((v)=>v && aclCheck("".concat(v.name, ":create"))), [
        enableChildren,
        totalChildCollections
    ]);
    const linkageRules = (null == fieldSchema ? void 0 : fieldSchema['x-linkage-rules']) || [];
    const values = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const localVariables = (0, external_variables_index_js_namespaceObject.useLocalVariables)({
        currentForm: {
            values
        }
    });
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { designable } = (0, external_index_js_namespaceObject.useDesignable)();
    const { styles } = useStyles();
    const icon = props.icon || null;
    const menuItems = (0, external_react_namespaceObject.useMemo)(()=>inheritsCollections.map((option)=>({
                key: option.name,
                label: compile(option.title),
                onClick: ()=>null == onClick ? void 0 : onClick(option)
            })), [
        inheritsCollections,
        onClick
    ]);
    const menu = (0, external_react_namespaceObject.useMemo)(()=>({
            items: menuItems
        }), [
        menuItems
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        field.stateOfLinkageRules = {};
        linkageRules.filter((k)=>!k.disabled).forEach((v)=>{
            var _v_actions;
            null === (_v_actions = v.actions) || void 0 === _v_actions || _v_actions.forEach((h)=>{
                (0, utils_js_namespaceObject.linkageAction)({
                    operator: h.operator,
                    field,
                    condition: v.condition,
                    variables,
                    localVariables
                });
            });
        });
    }, [
        field,
        linkageRules,
        localVariables,
        variables
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: styles.actionDesigner,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FinallyButton, {
            inheritsCollections,
            linkageFromForm,
            allowAddToCurrent,
            props,
            componentType,
            menu,
            onClick,
            collection,
            icon,
            field,
            form,
            designable
        })
    });
}, {
    displayName: 'CreateAction'
});
function FinallyButton(param) {
    let { inheritsCollections, linkageFromForm, allowAddToCurrent, props, componentType, menu, onClick, collection, icon, field, form, designable } = param;
    var _field_data, _field_data1;
    const { getCollection } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    if ((null == inheritsCollections ? void 0 : inheritsCollections.length) > 0) {
        var _field_data2, _field_data3;
        if (!linkageFromForm) return void 0 === allowAddToCurrent || allowAddToCurrent ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Dropdown.Button, {
            "aria-label": props['aria-label'],
            danger: props.danger,
            type: componentType,
            icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DownOutlined, {}),
            buttonsRender: (param)=>{
                let [leftButton, rightButton] = param;
                return [
                    /*#__PURE__*/ external_react_default().cloneElement(leftButton, {
                        style: null == props ? void 0 : props.style
                    }),
                    /*#__PURE__*/ external_react_default().cloneElement(rightButton, {
                        loading: false,
                        style: null == props ? void 0 : props.style
                    })
                ];
            },
            menu: menu,
            onClick: (info)=>{
                null == onClick || onClick(collection);
            },
            children: [
                icon,
                props.children
            ]
        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Dropdown, {
            menu: menu,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Button, {
                "aria-label": props['aria-label'],
                icon: icon,
                type: componentType,
                danger: props.danger,
                style: null == props ? void 0 : props.style,
                children: [
                    props.children,
                    " ",
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DownOutlined, {})
                ]
            })
        });
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
            "aria-label": props['aria-label'],
            type: componentType,
            disabled: field.disabled,
            danger: props.danger,
            icon: icon,
            onClick: (info)=>{
                const collectionName = getLinkageCollection(linkageFromForm, form, field);
                const targetCollectionName = inheritsCollections.find((v)=>v.name === collectionName) ? collectionName : collection.name;
                const targetCollection = getCollection(targetCollectionName);
                null == onClick || onClick(targetCollection);
            },
            style: {
                display: !designable && (null == field ? void 0 : null === (_field_data2 = field.data) || void 0 === _field_data2 ? void 0 : _field_data2.hidden) && 'none',
                opacity: designable && (null == field ? void 0 : null === (_field_data3 = field.data) || void 0 === _field_data3 ? void 0 : _field_data3.hidden) && 0.1
            },
            children: props.children
        });
    }
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
        ...props,
        "aria-label": props['aria-label'],
        type: componentType,
        disabled: field.disabled,
        danger: props.danger,
        icon: icon,
        onClick: (info)=>{
            null == onClick || onClick(collection);
        },
        style: {
            ...null == props ? void 0 : props.style,
            display: !designable && (null == field ? void 0 : null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.hidden) && 'none',
            opacity: designable && (null == field ? void 0 : null === (_field_data1 = field.data) || void 0 === _field_data1 ? void 0 : _field_data1.hidden) && 0.1
        },
        children: props.children
    });
}
const CreateRecordAction = /*#__PURE__*/ (0, external_react_namespaceObject.forwardRef)(InternalCreateRecordAction);
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
});
