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 createModalSettingsItem(options) {
    const { name, parentSchemaKey, valueKeys, schema, title, useSubmit = useHookDefault, useVisible, defaultValue: propsDefaultValue, useDefaultValue = useHookDefault, width, type = 'common' } = options;
    return {
        name,
        type: 'actionModal',
        useVisible,
        useComponentProps () {
            const fieldSchema = useSchemaByType(type);
            const { dn } = useDesignable();
            const defaultValue = useDefaultValue(propsDefaultValue);
            const values = parentSchemaKey ? lodash.get(fieldSchema, parentSchemaKey) : void 0;
            const compile = useCompile();
            const { t } = useTranslation();
            const onSubmit = useSubmit();
            const { fieldSchema: tableColumnSchema } = useColumnSchema() || {};
            return {
                title: 'function' == typeof title ? title(t) : compile(title),
                width,
                schema: schema({
                    ...defaultValue,
                    ...values
                }),
                onSubmit (values) {
                    const newSchema = getNewSchema({
                        fieldSchema,
                        parentSchemaKey: parentSchemaKey,
                        value: values,
                        valueKeys
                    });
                    if (tableColumnSchema) {
                        dn.emit('patch', {
                            schema: newSchema
                        });
                        dn.refresh();
                    } else dn.deepMerge(newSchema);
                    return null == onSubmit ? void 0 : onSubmit(values);
                }
            };
        }
    };
}
export { createModalSettingsItem };
