import { useMemo } from "react";
import { error } from "@tego/client";
import { useTranslation } from "react-i18next";
import { useCollectionManager_deprecated } from "../../../collection-manager/index.mjs";
import { useCompile, useGetFilterOptions } from "../../../schema-component/index.mjs";
const useIsSameOrChildCollection = ()=>{
    const { getChildrenCollections } = 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 } = useTranslation();
    const compile = useCompile();
    const getFilterOptions = useGetFilterOptions();
    const getIsSameOrChildCollection = useIsSameOrChildCollection();
    const loadChildren = (option)=>{
        var _option_field;
        if (!(null == (_option_field = option.field) ? void 0 : _option_field.target)) return new Promise((resolve)=>{
            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 = 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 };
