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";
function createSwitchSettingsItem(options) {
    const { name, useVisible, schemaKey, title, type = 'common', defaultValue: propsDefaultValue, useDefaultValue = useHookDefault } = options;
    return {
        name,
        useVisible,
        type: 'switch',
        useComponentProps () {
            const fieldSchema = useSchemaByType(type);
            const { dn } = useDesignable();
            const defaultValue = useDefaultValue(propsDefaultValue);
            const compile = useCompile();
            const { t } = useTranslation();
            const { fieldSchema: tableColumnSchema } = useColumnSchema() || {};
            return {
                title: 'function' == typeof title ? title(t) : compile(title),
                checked: !!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 { createSwitchSettingsItem };
