import { jsx } from "react/jsx-runtime";
import react from "react";
import { useFieldSchema } from "@tachybase/schema";
import { MenuOutlined } from "@ant-design/icons";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useAPIClient } from "../../../../api-client/index.mjs";
import { SchemaInitializerActionModal } from "../../../../application/schema-initializer/components/SchemaInitializerActionModal.mjs";
import { SchemaInitializerItem } from "../../../../application/schema-initializer/components/SchemaInitializerItem.mjs";
import { useSchemaInitializer } from "../../../../application/schema-initializer/context/index.mjs";
import { SchemaInitializer } from "../../../../application/schema-initializer/SchemaInitializer.mjs";
import { useCollection, useDataBlockProps } from "../../../../data-source/index.mjs";
import { createDesignable, useDesignable } from "../../../../schema-component/index.mjs";
import { useGetAriaLabelOfDesigner } from "../../../../schema-settings/hooks/useGetAriaLabelOfDesigner.mjs";
const Resizable = ()=>{
    var _fieldSchema_xcomponentprops;
    const { t } = useTranslation();
    const { dn } = useDesignable();
    const fieldSchema = useFieldSchema();
    return /*#__PURE__*/ jsx(SchemaInitializerActionModal, {
        title: t('Column width'),
        component: /*#__PURE__*/ react.forwardRef((props, ref)=>{
            const { children, onClick, ...others } = props;
            const { setVisible } = useSchemaInitializer();
            return /*#__PURE__*/ jsx(SchemaInitializerItem, {
                ref: ref,
                onClick: (param)=>{
                    let { event } = param;
                    setVisible(false);
                    onClick(event);
                },
                ...others,
                title: t('Column width')
            });
        }),
        schema: {
            type: 'object',
            title: t('Column width'),
            properties: {
                width: {
                    'x-decorator': 'FormItem',
                    'x-component': 'InputNumber',
                    'x-component-props': {},
                    default: (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.width) || 200
                }
            }
        },
        onSubmit: (param)=>{
            let { width } = param;
            const props = fieldSchema['x-component-props'] || {};
            props['width'] = width;
            const schema = {
                ['x-uid']: fieldSchema['x-uid']
            };
            schema['x-component-props'] = props;
            fieldSchema['x-component-props'] = props;
            dn.emit('patch', {
                schema
            });
            dn.refresh();
        }
    });
};
const commonOptions = {
    insertPosition: 'beforeEnd',
    useInsert: function() {
        const { refresh } = useDesignable();
        const fieldSchema = useFieldSchema();
        const api = useAPIClient();
        const { t } = useTranslation();
        return function(schema) {
            const spaceSchema = fieldSchema.reduceProperties((buf, schema)=>{
                if ('Space' === schema['x-component']) return schema;
                return buf;
            }, null);
            if (!spaceSchema) return;
            lodash.set(schema, 'x-designer-props.linkageAction', true);
            const dn = createDesignable({
                t,
                api,
                refresh,
                current: spaceSchema
            });
            dn.loadAPIClientEvents();
            dn.insertBeforeEnd(schema);
        };
    },
    Component: (props)=>{
        const { getAriaLabel } = useGetAriaLabelOfDesigner();
        return /*#__PURE__*/ jsx(MenuOutlined, {
            ...props,
            role: "button",
            "aria-label": getAriaLabel('schema-settings'),
            style: {
                cursor: 'pointer'
            }
        });
    },
    items: [
        {
            type: 'itemGroup',
            name: 'actions',
            title: '{{t("Enable actions")}}',
            children: [
                {
                    type: 'item',
                    title: '{{t("View")}}',
                    name: 'view',
                    Component: 'ViewActionInitializer',
                    schema: {
                        'x-component': 'Action.Link',
                        'x-action': 'view',
                        'x-decorator': 'ACLActionProvider'
                    }
                },
                {
                    type: 'item',
                    name: 'edit',
                    title: '{{t("Edit")}}',
                    Component: 'UpdateActionInitializer',
                    schema: {
                        'x-component': 'Action.Link',
                        'x-action': 'update',
                        'x-decorator': 'ACLActionProvider'
                    },
                    useVisible () {
                        const collection = useCollection();
                        return ('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template;
                    }
                },
                {
                    type: 'item',
                    title: '{{t("Delete")}}',
                    name: 'delete',
                    Component: 'DestroyActionInitializer',
                    schema: {
                        'x-component': 'Action.Link',
                        'x-action': 'destroy',
                        'x-decorator': 'ACLActionProvider'
                    },
                    useVisible () {
                        const collection = useCollection();
                        return ('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template;
                    }
                },
                {
                    type: 'item',
                    title: '{{t("Disassociate")}}',
                    name: 'disassociate',
                    Component: 'DisassociateActionInitializer',
                    schema: {
                        'x-component': 'Action.Link',
                        'x-action': 'disassociate',
                        'x-acl-action': 'destroy',
                        'x-decorator': 'ACLActionProvider'
                    },
                    useVisible () {
                        const props = useDataBlockProps();
                        const collection = useCollection();
                        return !!(null == props ? void 0 : props.association) && ('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template;
                    }
                },
                {
                    type: 'item',
                    title: '{{t("Add child")}}',
                    name: 'addChildren',
                    Component: 'CreateChildInitializer',
                    schema: {
                        'x-component': 'Action.Link',
                        'x-action': 'create',
                        'x-decorator': 'ACLActionProvider'
                    },
                    useVisible () {
                        var _fieldSchema_parent;
                        const fieldSchema = useFieldSchema();
                        const collection = useCollection();
                        const { treeTable } = (null == fieldSchema ? void 0 : null == (_fieldSchema_parent = fieldSchema.parent) ? void 0 : _fieldSchema_parent.parent['x-decorator-props']) || {};
                        return collection.tree && false !== treeTable;
                    }
                }
            ]
        },
        {
            name: 'divider',
            type: 'divider'
        },
        {
            type: 'subMenu',
            title: '{{t("Customize")}}',
            name: 'customize',
            children: [
                {
                    type: 'item',
                    title: '{{t("Popup")}}',
                    name: 'popup',
                    Component: 'PopupActionInitializer'
                },
                {
                    type: 'item',
                    title: '{{t("Update record")}}',
                    name: 'updateRecord',
                    Component: 'UpdateRecordActionInitializer',
                    useVisible () {
                        const collection = useCollection();
                        return ('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template;
                    }
                },
                {
                    name: 'customRequest',
                    title: '{{t("Custom request")}}',
                    Component: 'CustomRequestInitializer',
                    schema: {
                        'x-action': 'customize:table:request'
                    },
                    useVisible () {
                        const collection = useCollection();
                        return ('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template;
                    }
                }
            ]
        },
        {
            name: 'divider2',
            type: 'divider'
        },
        {
            type: 'item',
            name: 'columnWidth',
            title: 't("Column width")',
            Component: Resizable
        }
    ]
};
const tableActionColumnInitializers = new SchemaInitializer({
    name: 'table:configureItemActions',
    ...commonOptions
});
export { Resizable, tableActionColumnInitializers };
