import { useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useColumnSchema, useCompile, useDesignable } from "../../schema-component/index.mjs";
import { useCollectionField } from "../collection-field/CollectionFieldProvider.mjs";
import { useDataSourceManager } from "../data-source/DataSourceManagerProvider.mjs";
const fieldComponentSettingsItem = {
    name: 'fieldComponent',
    type: 'select',
    useVisible () {
        const collectionField = useCollectionField();
        const dm = useDataSourceManager();
        if (!collectionField) return false;
        const collectionInterface = dm.collectionFieldInterfaceManager.getFieldInterface(null == collectionField ? void 0 : collectionField.interface);
        return Array.isArray(null == collectionInterface ? void 0 : collectionInterface.componentOptions) && collectionInterface.componentOptions.length > 1 && collectionInterface.componentOptions.filter((item)=>!item.useVisible || item.useVisible()).length > 1;
    },
    useComponentProps () {
        var _collectionInterface_componentOptions_filter, _collectionInterface_componentOptions, _fieldSchema_xcomponentprops, _options_;
        const { t } = useTranslation();
        const field = useField();
        const schema = useFieldSchema();
        const collectionField = useCollectionField();
        const dm = useDataSourceManager();
        const collectionInterface = dm.collectionFieldInterfaceManager.getFieldInterface(null == collectionField ? void 0 : collectionField.interface);
        const { fieldSchema: tableColumnSchema } = useColumnSchema();
        const fieldSchema = tableColumnSchema || schema;
        const { dn } = useDesignable();
        const compile = useCompile();
        const options = (null == collectionInterface ? void 0 : null == (_collectionInterface_componentOptions = collectionInterface.componentOptions) ? void 0 : null == (_collectionInterface_componentOptions_filter = _collectionInterface_componentOptions.filter((item)=>!item.useVisible || item.useVisible())) ? void 0 : _collectionInterface_componentOptions_filter.map((item)=>({
                label: compile(item.label),
                value: item.value,
                useProps: item.useProps
            }))) || [];
        return {
            title: t('Field component'),
            options,
            value: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['component']) || (null == (_options_ = options[0]) ? void 0 : _options_.value),
            onChange (component) {
                var _componentOptions_useProps;
                const componentOptions = options.find((item)=>item.value === component);
                const componentProps = {
                    component,
                    ...(null == componentOptions ? void 0 : null == (_componentOptions_useProps = componentOptions.useProps) ? void 0 : _componentOptions_useProps.call(componentOptions)) || {}
                };
                field.componentProps = componentProps;
                field.component = component;
                lodash.set(fieldSchema, 'x-component-props', componentProps);
                dn.emit('patch', {
                    schema: {
                        ['x-uid']: fieldSchema['x-uid'],
                        'x-component-props': componentProps
                    }
                });
                dn.refresh();
            }
        };
    }
};
export { fieldComponentSettingsItem };
