import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { Select } from "antd";
import { useTranslation } from "react-i18next";
import { SchemaInitializerItem, SchemaInitializerSelect } from "../application/index.mjs";
import { useIsMobile } from "../block-provider/index.mjs";
import { PageStyle } from "../built-in/page-style/PageStyle.provider.mjs";
import { usePageStyle } from "../built-in/page-style/usePageStyle.mjs";
import { OpenMode, useDesignable } from "../schema-component/index.mjs";
import { SchemaSettingsSelectItem } from "../schema-settings/index.mjs";
const SchemaInitializerOpenModeSchemaItems = (options)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _fieldSchema_xcomponentprops2;
    const { openMode = true, openSize = true } = options;
    const fieldSchema = useFieldSchema();
    const field = useField();
    const { t } = useTranslation();
    const { dn } = useDesignable();
    const isMobile = useIsMobile();
    const pageStyle = usePageStyle();
    const defaultOpenMode = useMemo(()=>{
        if (isMobile || pageStyle === PageStyle.TAB_STYLE) return OpenMode.PAGE;
        return OpenMode.DRAWER_MODE;
    }, [
        isMobile,
        pageStyle
    ]);
    const openModeValue = (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['openMode']) || defaultOpenMode;
    var _fieldSchema_xcomponentprops_openSize;
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            openMode ? /*#__PURE__*/ jsx(SchemaInitializerSelect, {
                title: t('Open mode'),
                options: [
                    {
                        label: t('Drawer'),
                        value: OpenMode.DRAWER_MODE
                    },
                    {
                        label: t('Dialog'),
                        value: OpenMode.MODAL
                    }
                ],
                value: openModeValue,
                onChange: (value)=>{
                    field.componentProps.openMode = value;
                    const schema = {
                        'x-uid': fieldSchema['x-uid']
                    };
                    schema['x-component-props'] = fieldSchema['x-component-props'] || {};
                    schema['x-component-props'].openMode = value;
                    fieldSchema['x-component-props'].openMode = value;
                    Reflect.deleteProperty(fieldSchema['x-component-props'], 'openSize');
                    dn.emit('patch', {
                        schema: schema
                    });
                    dn.refresh();
                }
            }) : null,
            openSize && [
                OpenMode.MODAL,
                OpenMode.DRAWER_MODE
            ].includes(openModeValue) ? /*#__PURE__*/ jsx(SchemaInitializerItem, {
                children: /*#__PURE__*/ jsxs("div", {
                    style: {
                        alignItems: 'center',
                        display: 'flex',
                        justifyContent: 'space-between'
                    },
                    children: [
                        t('Popup size'),
                        /*#__PURE__*/ jsx(Select, {
                            "data-testid": "antd-select",
                            bordered: false,
                            options: [
                                {
                                    label: t('Small'),
                                    value: 'small'
                                },
                                {
                                    label: t('Middle'),
                                    value: 'middle'
                                },
                                {
                                    label: t('Large'),
                                    value: 'large'
                                }
                            ],
                            value: null != (_fieldSchema_xcomponentprops_openSize = null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops1['openSize']) ? _fieldSchema_xcomponentprops_openSize : (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops2 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops2['openMode']) === OpenMode.MODAL ? 'large' : 'middle',
                            onChange: (value)=>{
                                field.componentProps.openSize = value;
                                const schema = {
                                    'x-uid': fieldSchema['x-uid']
                                };
                                schema['x-component-props'] = fieldSchema['x-component-props'] || {};
                                schema['x-component-props'].openSize = value;
                                fieldSchema['x-component-props'].openSize = value;
                                dn.emit('patch', {
                                    schema: schema
                                });
                                dn.refresh();
                            },
                            style: {
                                textAlign: 'right',
                                minWidth: 100
                            }
                        })
                    ]
                })
            }) : null
        ]
    });
};
const SchemaSettingOpenModeSchemaItems = (options)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _fieldSchema_xcomponentprops2;
    const { openMode = true, openSize = true } = options;
    const fieldSchema = useFieldSchema();
    const field = useField();
    const { t } = useTranslation();
    const { dn } = useDesignable();
    const pageStyle = usePageStyle();
    const isMobile = useIsMobile();
    const defaultOpenMode = useMemo(()=>{
        if (isMobile || pageStyle === PageStyle.TAB_STYLE) return OpenMode.PAGE;
        return OpenMode.DRAWER_MODE;
    }, [
        isMobile,
        pageStyle
    ]);
    const openModeValue = (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['openMode']) || defaultOpenMode;
    var _fieldSchema_xcomponentprops_openSize;
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            openMode ? /*#__PURE__*/ jsx(SchemaSettingsSelectItem, {
                title: t('Open mode'),
                options: [
                    {
                        label: t('Drawer'),
                        value: OpenMode.DRAWER_MODE
                    },
                    {
                        label: t('Dialog'),
                        value: OpenMode.MODAL
                    },
                    {
                        label: t('Sheet'),
                        value: OpenMode.SHEET
                    },
                    {
                        label: t('Page'),
                        value: OpenMode.PAGE
                    }
                ],
                value: openModeValue,
                onChange: (value)=>{
                    field.componentProps.openMode = value;
                    const schema = {
                        'x-uid': fieldSchema['x-uid']
                    };
                    schema['x-component-props'] = fieldSchema['x-component-props'] || {};
                    schema['x-component-props'].openMode = value;
                    fieldSchema['x-component-props'].openMode = value;
                    Reflect.deleteProperty(fieldSchema['x-component-props'], 'openSize');
                    dn.emit('patch', {
                        schema: schema
                    });
                    dn.refresh();
                }
            }) : null,
            openSize && [
                OpenMode.MODAL,
                OpenMode.DRAWER_MODE
            ].includes(openModeValue) ? /*#__PURE__*/ jsx(SchemaSettingsSelectItem, {
                title: t('Popup size'),
                options: [
                    {
                        label: t('Small'),
                        value: 'small'
                    },
                    {
                        label: t('Middle'),
                        value: 'middle'
                    },
                    {
                        label: t('Large'),
                        value: 'large'
                    }
                ],
                value: null != (_fieldSchema_xcomponentprops_openSize = null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops1['openSize']) ? _fieldSchema_xcomponentprops_openSize : (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops2 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops2['openMode']) === OpenMode.MODAL ? 'large' : 'middle',
                onChange: (value)=>{
                    field.componentProps.openSize = value;
                    const schema = {
                        'x-uid': fieldSchema['x-uid']
                    };
                    schema['x-component-props'] = fieldSchema['x-component-props'] || {};
                    schema['x-component-props'].openSize = value;
                    fieldSchema['x-component-props'].openSize = value;
                    dn.emit('patch', {
                        schema: schema
                    });
                    dn.refresh();
                }
            }) : null
        ]
    });
};
export { SchemaInitializerOpenModeSchemaItems, SchemaSettingOpenModeSchemaItems };
