import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__ from "../data-source/data-source/DataSourceManagerProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__ from "../data-source/data-source/DataSourceProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_initializer_utils_mjs__ from "../schema-initializer/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useGetAriaLabelOfDesigner_mjs__ from "./hooks/useGetAriaLabelOfDesigner.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__ from "./SchemaSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__styles_mjs__ from "./styles.mjs";
const GeneralSchemaDesigner = (props)=>{
    var _rowCtx_cols, _ctx_renderSchemaInitializer;
    const { disableInitializer, title, template, schemaSettings, contextValue, draggable = true, showDataSource = true, AddMenuModalComponent } = props;
    const { dn, designable } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { getAriaLabel } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useGetAriaLabelOfDesigner_mjs__.useGetAriaLabelOfDesigner)();
    const schemaSettingsProps = {
        dn,
        field,
        fieldSchema
    };
    const { render: schemaSettingsRender, exists: schemaSettingsExists } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaSettingsRender)(fieldSchema['x-settings'] || schemaSettings, fieldSchema['x-settings-props']);
    const rowCtx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useGridRowContext)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useGridContext)();
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__styles_mjs__.useStyles)();
    const dm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__.useDataSourceManager)();
    const dataSources = null == dm ? void 0 : dm.getDataSources();
    const dataSourceContext = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__.useDataSource)();
    const dataSource = (null == dataSources ? void 0 : dataSources.length) > 1 && dataSourceContext;
    const templateName = [
        'FormItem',
        'ReadPrettyFormItem'
    ].includes(null == template ? void 0 : template.componentName) ? "".concat(null == template ? void 0 : template.name, " ").concat(t('(Fields only)')) : null == template ? void 0 : template.name;
    const initializerProps = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _rowCtx_cols;
        return {
            insertPosition: 'afterEnd',
            wrap: (null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length) > 1 ? void 0 : __WEBPACK_EXTERNAL_MODULE__schema_initializer_utils_mjs__.gridRowColWrap,
            Component: (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {
                    ...props,
                    role: "button",
                    "aria-label": getAriaLabel('schema-initializer'),
                    style: {
                        cursor: 'pointer',
                        fontSize: 14
                    }
                })
        };
    }, [
        getAriaLabel,
        null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length
    ]);
    if (!designable) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaToolbarProvider, {
        ...contextValue,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])('general-schema-designer', styles.overrideAntdCSS),
            children: [
                title && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])('general-schema-designer-title', styles.titleCss),
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                        size: 2,
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                                className: 'title-tag',
                                children: showDataSource && dataSource ? "".concat(compile(null == dataSource ? void 0 : dataSource.displayName), " > ").concat(compile(title)) : compile(title)
                            }),
                            template && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("span", {
                                className: 'title-tag',
                                children: [
                                    t('Reference template'),
                                    ": ",
                                    templateName || t('Untitled')
                                ]
                            })
                        ]
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    className: 'general-schema-designer-icons',
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                        size: 3,
                        align: 'center',
                        children: [
                            !!AddMenuModalComponent && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.SchemaSettingsDropdown, {
                                title: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {
                                    role: "button",
                                    "aria-label": getAriaLabel('add-menu')
                                }),
                                ...schemaSettingsProps,
                                children: AddMenuModalComponent
                            }),
                            draggable && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.DragHandler, {
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DragOutlined, {
                                    role: "button",
                                    "aria-label": getAriaLabel('drag-handler')
                                })
                            }),
                            !disableInitializer && ((null == ctx ? void 0 : ctx.InitializerComponent) ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ctx.InitializerComponent, {
                                ...initializerProps
                            }) : null == ctx ? void 0 : null === (_ctx_renderSchemaInitializer = ctx.renderSchemaInitializer) || void 0 === _ctx_renderSchemaInitializer ? void 0 : _ctx_renderSchemaInitializer.call(ctx, initializerProps)),
                            schemaSettingsExists ? schemaSettingsRender(contextValue) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.SchemaSettingsDropdown, {
                                title: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.MenuOutlined, {
                                    role: "button",
                                    "aria-label": getAriaLabel('schema-settings'),
                                    style: {
                                        cursor: 'pointer',
                                        fontSize: 12
                                    }
                                }),
                                ...schemaSettingsProps,
                                children: props.children
                            })
                        ]
                    })
                })
            ]
        })
    });
};
const InternalSchemaToolbar = (props)=>{
    var _rowCtx_cols;
    const { title, initializer, settings, showBackground, showBorder = true, draggable = true } = props;
    const { designable } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__styles_mjs__.useStyles)();
    const { getAriaLabel } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useGetAriaLabelOfDesigner_mjs__.useGetAriaLabelOfDesigner)();
    const dm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__.useDataSourceManager)();
    const dataSources = null == dm ? void 0 : dm.getDataSources();
    const dataSourceContext = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__.useDataSource)();
    const dataSource = (null == dataSources ? void 0 : dataSources.length) > 1 && dataSourceContext;
    const titleArr = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (!title) return;
        if ('string' == typeof title) return [
            compile(title)
        ];
        if (Array.isArray(title)) return title.map((item)=>compile(item));
    }, [
        compile,
        title
    ]);
    const { render: schemaSettingsRender, exists: schemaSettingsExists } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaSettingsRender)(fieldSchema['x-settings'] || settings, fieldSchema['x-settings-props']);
    const { render: schemaInitializerRender, exists: schemaInitializerExists } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerRender)(fieldSchema['x-initializer'] || initializer, fieldSchema['x-initializer-props']);
    const rowCtx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useGridRowContext)();
    const gridContext = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useGridContext)();
    const initializerProps = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _rowCtx_cols;
        return {
            insertPosition: 'afterEnd',
            wrap: (null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length) > 1 ? void 0 : __WEBPACK_EXTERNAL_MODULE__schema_initializer_utils_mjs__.gridRowColWrap,
            Component: (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {
                    ...props,
                    role: "button",
                    "aria-label": getAriaLabel('schema-initializer'),
                    style: {
                        cursor: 'pointer',
                        fontSize: 14
                    }
                })
        };
    }, [
        getAriaLabel,
        null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length
    ]);
    const dragElement = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (false === draggable) return null;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.DragHandler, {
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DragOutlined, {
                role: "button",
                "aria-label": getAriaLabel('drag-handler')
            })
        });
    }, [
        draggable,
        getAriaLabel
    ]);
    const initializerElement = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (false === initializer) return null;
        if ((null == gridContext ? void 0 : gridContext.InitializerComponent) || (null == gridContext ? void 0 : gridContext.renderSchemaInitializer)) {
            var _gridContext_renderSchemaInitializer;
            return (null == gridContext ? void 0 : gridContext.InitializerComponent) ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(gridContext.InitializerComponent, {
                ...initializerProps
            }) : null === (_gridContext_renderSchemaInitializer = gridContext.renderSchemaInitializer) || void 0 === _gridContext_renderSchemaInitializer ? void 0 : _gridContext_renderSchemaInitializer.call(gridContext, initializerProps);
        }
        if (!schemaInitializerExists) return null;
        return schemaInitializerRender(initializerProps);
    }, [
        gridContext,
        initializer,
        initializerProps,
        schemaInitializerExists,
        schemaInitializerRender
    ]);
    const settingsElement = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>false !== settings && schemaSettingsExists ? schemaSettingsRender() : null, [
        schemaSettingsExists,
        schemaSettingsRender,
        settings
    ]);
    const toolbarRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const toolbarElement = toolbarRef.current;
        const parentElement = null == toolbarElement ? void 0 : toolbarElement.parentElement;
        function show() {
            if (toolbarElement) toolbarElement.style.display = 'block';
        }
        function hide() {
            if (toolbarElement) toolbarElement.style.display = 'none';
        }
        if (parentElement) {
            const style = window.getComputedStyle(parentElement);
            if ('static' === style.position) parentElement.style.position = 'relative';
            parentElement.addEventListener('mouseenter', show);
            parentElement.addEventListener('mouseleave', hide);
        }
        return ()=>{
            if (parentElement) {
                parentElement.removeEventListener('mouseenter', show);
                parentElement.removeEventListener('mouseleave', hide);
            }
        };
    }, []);
    if (!designable) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        ref: toolbarRef,
        className: styles.toolbar,
        style: {
            border: showBorder ? 'auto' : 0,
            background: showBackground ? 'auto' : 0
        },
        children: [
            titleArr && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                className: styles.toolbarTitle,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                    size: 2,
                    children: titleArr.map((item)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                            className: styles.toolbarTitleTag,
                            children: dataSource ? "".concat(compile(null == dataSource ? void 0 : dataSource.displayName), " > ").concat(item) : item
                        }, item))
                })
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                className: styles.toolbarIcons,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                    size: 3,
                    align: 'center',
                    children: [
                        dragElement,
                        initializerElement,
                        settingsElement
                    ]
                })
            })
        ]
    });
};
const SchemaToolbar = (props)=>{
    const { designable } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    if (!designable) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalSchemaToolbar, {
        ...props
    });
};
export { GeneralSchemaDesigner, SchemaToolbar };
