"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__, {
    useBaseVariable: ()=>useBaseVariable,
    BaseVariableProvider: ()=>BaseVariableProvider
});
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 index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../schema-component/index.js");
const useSpecialCase_js_namespaceObject = require("../../../schema-component/antd/form-item/hooks/useSpecialCase.js");
const BaseVariableContext = /*#__PURE__*/ external_react_default().createContext(null);
const BaseVariableProvider = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseVariableContext.Provider, {
        value: props,
        children: props.children
    });
const getChildren = (options, param)=>{
    let { collectionField, uiSchema, depth, maxDepth, noDisabled, loadChildren, compile, isDisabled, targetFieldSchema, getCollectionField } = param;
    const result = options.map((option)=>{
        if (!option.target) return {
            key: option.name,
            value: option.name,
            label: compile(option.title),
            disabled: !noDisabled && isDisabled({
                option,
                collectionField,
                uiSchema,
                targetFieldSchema,
                getCollectionField
            }),
            isLeaf: true,
            depth
        };
        if (depth >= maxDepth) return null;
        return {
            key: option.name,
            value: option.name,
            label: compile(option.title),
            isLeaf: false,
            field: option,
            depth,
            disabled: !noDisabled && isDisabled({
                option,
                collectionField,
                uiSchema,
                targetFieldSchema,
                getCollectionField
            }),
            loadChildren
        };
    }).filter(Boolean);
    return result;
};
const useBaseVariable = (param)=>{
    let { collectionField, uiSchema, targetFieldSchema, maxDepth = 3, name, title, collectionName, noChildren = false, noDisabled = true, dataSource, returnFields = (fields)=>fields } = param;
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const getFilterOptions = (0, external_schema_component_index_js_namespaceObject.useGetFilterOptions)();
    const { isDisabled } = (0, external_react_namespaceObject.useContext)(BaseVariableContext) || {};
    const { getCollectionField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)(dataSource);
    const loadChildren = (option)=>{
        var _option_field;
        if (!(null === (_option_field = option.field) || void 0 === _option_field ? void 0 : _option_field.target)) return Promise.resolve(void 0);
        const target = option.field.target;
        return new Promise((resolve)=>{
            setTimeout(()=>{
                const children = (getChildren(returnFields(getFilterOptions(target, dataSource), option), {
                    collectionField,
                    uiSchema,
                    targetFieldSchema,
                    depth: option.depth + 1,
                    maxDepth,
                    noDisabled,
                    loadChildren,
                    compile,
                    isDisabled: isDisabled || isDisabledDefault,
                    getCollectionField
                }) || []).sort((a, b)=>{
                    if (a.isLeaf && !b.isLeaf) return -1;
                    if (!a.isLeaf && b.isLeaf) return 1;
                    return 0;
                }).sort((a, b)=>{
                    if (a.disabled && !b.disabled) return 1;
                    if (!a.disabled && b.disabled) return -1;
                    return 0;
                });
                if (0 === children.length) {
                    option.disabled = true;
                    option.isLeaf = true;
                    resolve();
                    return;
                }
                option.children = children;
                resolve();
            }, 5);
        });
    };
    const result = (0, external_react_namespaceObject.useMemo)(()=>({
            label: title,
            value: name,
            key: name,
            isLeaf: noChildren,
            field: {
                target: collectionName
            },
            depth: 0,
            loadChildren,
            children: []
        }), [
        null == uiSchema ? void 0 : uiSchema['x-component']
    ]);
    return result;
};
function isDisabledDefault(params) {
    var _option_schema;
    const { option, collectionField, uiSchema, targetFieldSchema, getCollectionField } = params;
    if (!uiSchema || !collectionField) return true;
    if ('json' === collectionField.interface) return false;
    if (!collectionField.target && [
        'hasMany',
        'belongsToMany'
    ].includes(option.type)) return true;
    if (!collectionField.target && [
        'hasOne',
        'belongsTo'
    ].includes(option.type)) return false;
    if (option.target && (0, useSpecialCase_js_namespaceObject.isSpecialCaseField)({
        collectionField,
        fieldSchema: targetFieldSchema,
        getCollectionField
    })) return false;
    if ([
        'hasOne',
        'belongsTo'
    ].includes(collectionField.type) && [
        'hasMany',
        'belongsToMany'
    ].includes(option.type)) return true;
    if ([
        'hasOne',
        'belongsTo'
    ].includes(collectionField.type) && [
        'hasOne',
        'belongsTo'
    ].includes(option.type)) return false;
    if ([
        'hasMany',
        'belongsToMany'
    ].includes(collectionField.type) && option.target) return false;
    if ([
        'input',
        'markdown',
        'richText',
        'textarea',
        'username'
    ].includes(collectionField.interface)) {
        var _option_schema1;
        return ![
            'string',
            'number'
        ].includes(null === (_option_schema1 = option.schema) || void 0 === _option_schema1 ? void 0 : _option_schema1.type);
    }
    if (collectionField.interface && option.interface) return collectionField.interface !== option.interface;
    if ((null == uiSchema ? void 0 : uiSchema['x-component']) !== (null === (_option_schema = option.schema) || void 0 === _option_schema ? void 0 : _option_schema['x-component'])) return true;
    return false;
}
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
});
