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__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_hooks_useSpecialCase_mjs__ from "../../../schema-component/antd/form-item/hooks/useSpecialCase.mjs";
const BaseVariableContext = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createContext(null);
const BaseVariableProvider = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const getFilterOptions = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useGetFilterOptions)();
    const { isDisabled } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BaseVariableContext) || {};
    const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_hooks_useSpecialCase_mjs__.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;
}
export { BaseVariableProvider, useBaseVariable };
