import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_field_CollectionFieldProvider_mjs__ from "../collection-field/CollectionFieldProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_DataSourceManagerProvider_mjs__ from "../data-source/DataSourceManagerProvider.mjs";
const fieldComponentSettingsItem = {
    name: 'fieldComponent',
    type: 'select',
    useVisible () {
        const collectionField = (0, __WEBPACK_EXTERNAL_MODULE__collection_field_CollectionFieldProvider_mjs__.useCollectionField)();
        const dm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_DataSourceManagerProvider_mjs__.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 } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const collectionField = (0, __WEBPACK_EXTERNAL_MODULE__collection_field_CollectionFieldProvider_mjs__.useCollectionField)();
        const dm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_DataSourceManagerProvider_mjs__.useDataSourceManager)();
        const collectionInterface = dm.collectionFieldInterfaceManager.getFieldInterface(null == collectionField ? void 0 : collectionField.interface);
        const { fieldSchema: tableColumnSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useColumnSchema)();
        const fieldSchema = tableColumnSchema || schema;
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
        const options = (null == collectionInterface ? void 0 : null === (_collectionInterface_componentOptions = collectionInterface.componentOptions) || void 0 === _collectionInterface_componentOptions ? void 0 : null === (_collectionInterface_componentOptions_filter = _collectionInterface_componentOptions.filter((item)=>!item.useVisible || item.useVisible())) || void 0 === _collectionInterface_componentOptions_filter ? 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 ? void 0 : _fieldSchema_xcomponentprops['component']) || (null === (_options_ = options[0]) || void 0 === _options_ ? 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 ? void 0 : _componentOptions_useProps.call(componentOptions)) || {}
                };
                field.componentProps = componentProps;
                field.component = component;
                __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(fieldSchema, 'x-component-props', componentProps);
                dn.emit('patch', {
                    schema: {
                        ['x-uid']: fieldSchema['x-uid'],
                        'x-component-props': componentProps
                    }
                });
                dn.refresh();
            }
        };
    }
};
export { fieldComponentSettingsItem };
