import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { useDesignable } from "../../index.mjs";
import { SchemaSettings } from "../../../application/schema-settings/index.mjs";
import { useSchemaToolbar } from "../../../application/schema-toolbar/index.mjs";
function useNotDisableHeader() {
    var _fieldSchema_xcomponentprops;
    const fieldSchema = useFieldSchema();
    return !(null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.disablePageHeader);
}
const pageSettings = new SchemaSettings({
    name: 'PageSettings',
    items: [
        {
            name: 'enablePageHeader',
            type: 'switch',
            useComponentProps () {
                var _fieldSchema_xcomponentprops;
                const { dn } = useDesignable();
                const { t } = useTranslation();
                const fieldSchema = useFieldSchema();
                return {
                    title: t('Enable page header'),
                    checked: !(null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.disablePageHeader),
                    onChange (v) {
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props']['disablePageHeader'] = !v;
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                ['x-component-props']: fieldSchema['x-component-props']
                            }
                        });
                        dn.refresh();
                    }
                };
            }
        },
        {
            name: 'divider',
            type: 'divider',
            useVisible: useNotDisableHeader
        },
        {
            name: 'displayPageTitle',
            type: 'switch',
            useVisible: useNotDisableHeader,
            useComponentProps () {
                var _fieldSchema_xcomponentprops;
                const { dn } = useDesignable();
                const { t } = useTranslation();
                const fieldSchema = useFieldSchema();
                return {
                    title: t('Display page title'),
                    checked: !(null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.hidePageTitle),
                    onChange (v) {
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props']['hidePageTitle'] = !v;
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                ['x-component-props']: fieldSchema['x-component-props']
                            }
                        });
                        dn.refresh();
                    }
                };
            }
        },
        {
            name: 'editPageTitle',
            type: 'modal',
            useVisible () {
                var _fieldSchema_xcomponentprops;
                const notDisablePageHeader = useNotDisableHeader();
                const fieldSchema = useFieldSchema();
                const notHidePageTitle = !(null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.hidePageTitle);
                return notDisablePageHeader && notHidePageTitle;
            },
            useComponentProps () {
                const { dn } = useDesignable();
                const { t } = useTranslation();
                const field = useField();
                const fieldSchema = useFieldSchema();
                const { title } = useSchemaToolbar();
                return {
                    hide: true,
                    title: t('Edit page title'),
                    schema: {
                        type: 'object',
                        title: t('Edit page title'),
                        properties: {
                            title: {
                                title: t('Title'),
                                required: true,
                                'x-decorator': 'FormItem',
                                'x-component': 'Input',
                                'x-component-props': {}
                            }
                        }
                    },
                    initialValues: {
                        title
                    },
                    onSubmit (param) {
                        let { title } = param;
                        field.title = title;
                        fieldSchema['title'] = title;
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                title
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'enablePageTabs',
            type: 'switch',
            useVisible: useNotDisableHeader,
            useComponentProps () {
                var _fieldSchema_xcomponentprops;
                const { dn } = useDesignable();
                const { t } = useTranslation();
                const fieldSchema = useFieldSchema();
                return {
                    title: t('Enable page tabs'),
                    checked: null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.enablePageTabs,
                    onChange (v) {
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props']['enablePageTabs'] = v;
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                ['x-component-props']: fieldSchema['x-component-props']
                            }
                        });
                        dn.refresh();
                    }
                };
            }
        }
    ]
});
export { pageSettings };
