"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    useIsSameOrChildCollection: ()=>useIsSameOrChildCollection,
    useContextAssociationFields: ()=>useContextAssociationFields
});
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/utils/client");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../schema-component/index.js");
const useIsSameOrChildCollection = ()=>{
    const { getChildrenCollections } = (0, index_js_namespaceObject.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, external_react_i18next_namespaceObject.useTranslation)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const getFilterOptions = (0, external_schema_component_index_js_namespaceObject.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, client_namespaceObject.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, external_react_namespaceObject.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;
};
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
});
