import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useColumnSchema, useCompile, useDesignable } from "../../schema-component/index.mjs";
import { getNewSchema, useHookDefault, useSchemaByType } from "./util.mjs";
const createSelectSchemaSettingsItem = (options)=>{
    const { name, title, options: propsOptions, useOptions = useHookDefault, schemaKey, useVisible, type = 'common', defaultValue: propsDefaultValue, useDefaultValue = useHookDefault } = options;
    return {
        name,
        type: 'select',
        useVisible,
        useComponentProps () {
            const fieldSchema = useSchemaByType(type);
            const { fieldSchema: tableColumnSchema } = useColumnSchema() || {};
            const { dn } = useDesignable();
            const options = useOptions(propsOptions);
            const defaultValue = useDefaultValue(propsDefaultValue);
            const compile = useCompile();
            const { t } = useTranslation();
            return {
                title: 'function' == typeof title ? title(t) : compile(title),
                options,
                value: lodash.get(fieldSchema, schemaKey, defaultValue),
                onChange (v) {
                    const newSchema = getNewSchema({
                        fieldSchema,
                        schemaKey,
                        value: v
                    });
                    if (tableColumnSchema) {
                        dn.emit('patch', {
                            schema: newSchema
                        });
                        dn.refresh();
                    } else dn.deepMerge(newSchema);
                }
            };
        }
    };
};
export { createSelectSchemaSettingsItem };
