import { jsx } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { ArrayItems } from "@tego/client";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useCollectionRecord, useDesignable } from "../../../index.mjs";
import { useSchemaToolbar } from "../../../application/index.mjs";
import { SchemaSettings } from "../../../application/schema-settings/SchemaSettings.mjs";
import { useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { ButtonEditor, RemoveButton } from "../../../schema-component/antd/action/Action.Designer.mjs";
import { SchemaSettingsLinkageRules, SchemaSettingsModalItem } from "../../../schema-settings/index.mjs";
import { useURLAndHTMLSchema } from "./hooks.mjs";
const SchemaSettingsActionLinkItem = ()=>{
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { dn } = useDesignable();
    const { t } = useTranslation();
    const { urlSchema, paramsSchema, openInNewWindowSchema } = useURLAndHTMLSchema();
    const componentProps = fieldSchema['x-component-props'] || {};
    const initialValues = {
        url: componentProps.url,
        params: componentProps.params || [
            {}
        ],
        openInNewWindow: componentProps.openInNewWindow
    };
    return /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
        title: t('Edit link'),
        components: {
            ArrayItems: ArrayItems
        },
        schema: {
            type: 'object',
            title: t('Edit link'),
            properties: {
                url: {
                    ...urlSchema,
                    required: true
                },
                params: paramsSchema,
                openInNewWindow: openInNewWindowSchema
            }
        },
        onSubmit: (param)=>{
            let { url, params, openInNewWindow } = param;
            componentProps.url = url;
            componentProps.params = params;
            componentProps.openInNewWindow = openInNewWindow;
            fieldSchema['x-component-props'] = componentProps;
            field.componentProps.url = url;
            field.componentProps.params = params;
            field.componentProps.openInNewWindow = openInNewWindow;
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-component-props': componentProps
                }
            });
            dn.refresh();
        },
        initialValues: initialValues
    });
};
const customizeLinkActionSettings = new SchemaSettings({
    name: 'actionSettings:link',
    items: [
        {
            name: 'editButton',
            Component: ButtonEditor,
            useComponentProps () {
                const { buttonEditorProps } = useSchemaToolbar();
                return buttonEditorProps;
            }
        },
        {
            name: 'editLink',
            Component: SchemaSettingsActionLinkItem
        },
        {
            name: 'linkageRules',
            Component: SchemaSettingsLinkageRules,
            useVisible () {
                const record = useCollectionRecord();
                return !lodash.isEmpty(null == record ? void 0 : record.data);
            },
            useComponentProps () {
                const { name } = useCollection_deprecated();
                const { linkageRulesProps } = useSchemaToolbar();
                return {
                    ...linkageRulesProps,
                    collectionName: name
                };
            }
        },
        {
            name: 'remove',
            sort: 100,
            Component: RemoveButton,
            useComponentProps () {
                const { removeButtonProps } = useSchemaToolbar();
                return removeButtonProps;
            }
        }
    ]
});
export { SchemaSettingsActionLinkItem, customizeLinkActionSettings };
