import { jsx, jsxs } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { useDesignable } from "../../index.mjs";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { GeneralSchemaDesigner, SchemaSettingsDivider, SchemaSettingsItemGroup, SchemaSettingsModalItem, SchemaSettingsRemove, SchemaSettingsSwitchItem } from "../../../schema-settings/index.mjs";
import { useCompile } from "../../hooks/index.mjs";
const useFilterableFields = (collectionName)=>{
    var _fields_filter;
    const { getCollectionFields, getInterface } = useCollectionManager_deprecated();
    const fields = getCollectionFields(collectionName);
    return null == fields ? void 0 : null == (_fields_filter = fields.filter) ? void 0 : _fields_filter.call(fields, (field)=>{
        if (!field.interface) return false;
        const fieldInterface = getInterface(field.interface);
        if (!(null == fieldInterface ? void 0 : fieldInterface.filterable)) return false;
        return true;
    });
};
const FilterableFieldsSchemaSettingsItem = ()=>{
    var _fieldSchema_xcomponentprops;
    const fieldSchema = useFieldSchema();
    const { dn } = useDesignable();
    const { name } = useCollection_deprecated();
    const fields = useFilterableFields(name);
    const compile = useCompile();
    const { t } = useTranslation();
    const nonfilterable = (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.nonfilterable) || [];
    return /*#__PURE__*/ jsx(SchemaSettingsItemGroup, {
        title: t('Filterable fields'),
        children: fields.map((field)=>{
            var _field_uiSchema;
            const checked = !nonfilterable.includes(field.name);
            return /*#__PURE__*/ jsx(SchemaSettingsSwitchItem, {
                checked: checked,
                title: compile(null == field ? void 0 : null == (_field_uiSchema = field.uiSchema) ? void 0 : _field_uiSchema.title),
                onChange: (value)=>{
                    var _fieldSchema_xcomponentprops;
                    fieldSchema['x-component-props'] = (null == fieldSchema ? void 0 : fieldSchema['x-component-props']) || {};
                    const nonfilterable = (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.nonfilterable) || [];
                    if (value) {
                        const index = nonfilterable.indexOf(field.name);
                        nonfilterable.splice(index, 1);
                    } else nonfilterable.push(field.name);
                    fieldSchema['x-component-props'].nonfilterable = nonfilterable;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-component-props': {
                                ...fieldSchema['x-component-props']
                            }
                        }
                    });
                    dn.refresh();
                }
            }, field.name);
        })
    });
};
const FilterActionDesigner = (props)=>{
    var _fieldSchema_xcomponentprops;
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { dn } = useDesignable();
    const { t } = useTranslation();
    return /*#__PURE__*/ jsxs(GeneralSchemaDesigner, {
        ...props,
        disableInitializer: true,
        children: [
            /*#__PURE__*/ jsx(FilterableFieldsSchemaSettingsItem, {}),
            /*#__PURE__*/ jsx(SchemaSettingsDivider, {}),
            /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
                title: t('Edit button'),
                schema: {
                    type: 'object',
                    title: t('Edit button'),
                    properties: {
                        title: {
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            title: t('Button title'),
                            default: fieldSchema.title,
                            'x-component-props': {}
                        },
                        icon: {
                            'x-decorator': 'FormItem',
                            'x-component': 'IconPicker',
                            title: t('Button icon'),
                            default: null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.icon,
                            'x-component-props': {}
                        }
                    }
                },
                onSubmit: (param)=>{
                    let { title, icon } = param;
                    fieldSchema.title = title;
                    field.title = title;
                    field.componentProps.icon = icon;
                    fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                    fieldSchema['x-component-props'].icon = icon;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            title,
                            'x-component-props': {
                                ...fieldSchema['x-component-props']
                            }
                        }
                    });
                    dn.refresh();
                }
            }),
            /*#__PURE__*/ jsx(SchemaSettingsDivider, {}),
            /*#__PURE__*/ jsx(SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: (s)=>'Space' === s['x-component'] || 'ActionBar' === s['x-component']
            })
        ]
    });
};
export { FilterActionDesigner, FilterableFieldsSchemaSettingsItem, useFilterableFields };
