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 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, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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;
}
export { useVariableOptions };
