import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
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";
const useIsSameOrChildCollection = ()=>{
    const { getChildrenCollections } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    return (contextCollection, targetCollection)=>{
        if (contextCollection === targetCollection) return true;
        const children = getChildrenCollections(targetCollection);
        return null == children ? void 0 : children.some((v)=>v.name === contextCollection);
    };
};
const getChildren = (options, param, collectionField, getIsSameOrChildCollection)=>{
    let { schema, depth, maxDepth, loadChildren, compile } = param;
    const result = options.map((option)=>{
        const disabled = !getIsSameOrChildCollection(option.target, null == collectionField ? void 0 : collectionField.target);
        if (!option.target) return {
            key: option.name,
            value: option.name,
            label: compile(option.title),
            disabled: disabled,
            isLeaf: true,
            depth
        };
        if (depth >= maxDepth) return null;
        return {
            key: option.name,
            value: option.name,
            label: compile(option.title),
            disabled: disabled,
            isLeaf: true,
            field: option,
            depth,
            loadChildren
        };
    }).filter(Boolean);
    return result;
};
const useContextAssociationFields = (param)=>{
    let { schema, maxDepth = 3, contextCollectionName, collectionField } = param;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const getFilterOptions = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useGetFilterOptions)();
    const getIsSameOrChildCollection = useIsSameOrChildCollection();
    const loadChildren = (option)=>{
        var _option_field;
        if (!(null === (_option_field = option.field) || void 0 === _option_field ? void 0 : _option_field.target)) return new Promise((resolve)=>{
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.error)('Must be set field target');
            option.children = [];
            resolve(void 0);
        });
        const collectionName = option.field.target;
        return new Promise((resolve)=>{
            setTimeout(()=>{
                const children = getChildren(getFilterOptions(collectionName).filter((v)=>{
                    const isAssociationField = [
                        'hasOne',
                        'hasMany',
                        'belongsTo',
                        'belongsToMany'
                    ].includes(v.type);
                    return isAssociationField;
                }), {
                    schema,
                    depth: option.depth + 1,
                    maxDepth,
                    loadChildren,
                    compile
                }, collectionField, getIsSameOrChildCollection) || [];
                if (0 === children.length) {
                    option.disabled = true;
                    option.children = [];
                    resolve();
                    return;
                }
                option.children = children;
                resolve();
            }, 5);
        });
    };
    const result = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            label: t('Table selected records'),
            value: '$context',
            key: '$context',
            isLeaf: false,
            field: {
                target: contextCollectionName
            },
            depth: 0,
            loadChildren
        }), [
        null == schema ? void 0 : schema['x-component']
    ]);
    return result;
};
export { useContextAssociationFields, useIsSameOrChildCollection };
