"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__, {
    useVariableOptions: ()=>useVariableOptions
});
const index_js_namespaceObject = require("../../collection-manager/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const supportsType = [
    'id',
    'checkbox',
    'number',
    'percent',
    'integer',
    'input',
    'textarea',
    'email',
    'phone',
    'datetime',
    'createdAt',
    'updatedAt',
    'radioGroup',
    'checkboxGroup',
    'select',
    'multipleSelect',
    'formula',
    'oho',
    'obo',
    'm2o',
    'o2m',
    'm2m'
];
const useVariableTypes = function(currentCollection) {
    let excludes = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [];
    const { getCollectionFields, getInterface, getCollection } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const collection = getCollection(currentCollection);
    const fields = getCollectionFields(currentCollection);
    return [
        {
            title: collection.title,
            value: currentCollection,
            options () {
                const field2option = (field, depth)=>{
                    var _field_uiSchema;
                    if (!field.interface || !supportsType.filter((v)=>!excludes.includes(v)).includes(field.interface)) return;
                    const fieldInterface = getInterface(field.interface);
                    if (!(null == fieldInterface ? void 0 : fieldInterface.filterable)) return;
                    const { nested, children } = fieldInterface.filterable;
                    const option = {
                        key: field.name,
                        label: (null == field ? void 0 : field.title) || (null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title),
                        schema: null == field ? void 0 : field.uiSchema,
                        value: field.name
                    };
                    if (field.target && depth > 1) return;
                    if (depth > 1) return option;
                    if (null == children ? void 0 : children.length) option['children'] = children;
                    if (nested) {
                        const targetFields = getCollectionFields(field.target);
                        const options = getOptions(targetFields, depth + 1).filter(Boolean);
                        option['children'] = option['children'] || [];
                        option['children'].push(...options);
                    }
                    return option;
                };
                const getOptions = (fields, depth)=>{
                    const options = [];
                    fields.forEach((field)=>{
                        const option = field2option(field, depth);
                        if (option) options.push(option);
                    });
                    return options;
                };
                return getOptions(fields, 1);
            }
        },
        {
            title: '{{t("System variables")}}',
            value: '$system',
            options: [
                {
                    key: 'now',
                    value: 'now',
                    label: '{{t("Current time")}}'
                }
            ]
        }
    ];
};
function useVariableOptions(collectionName, excludes) {
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const options = useVariableTypes(collectionName, excludes).map((item)=>{
        const options = 'function' == typeof item.options ? item.options() : item.options;
        return {
            label: compile(item.title),
            value: item.value,
            key: item.value,
            children: compile(options),
            disabled: options && !options.length
        };
    });
    return options;
}
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
});
