import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { uid, useForm } from "@tachybase/schema";
import { App } from "antd";
import { useTranslation } from "react-i18next";
import { useAPIClient, useRequest } from "../../../../api-client/index.mjs";
import { SchemaComponent } from "../../../../schema-component/index.mjs";
const PluginUrlForm = (param)=>{
    let { onClose, pluginData, isUpgrade } = param;
    const { message } = App.useApp();
    const useSaveValues = ()=>{
        const api = useAPIClient();
        const { t } = useTranslation();
        const form = useForm();
        return {
            async run () {
                const compressedFileUrl = form.values.compressedFileUrl;
                if (!compressedFileUrl) return;
                await form.submit();
                const data = {
                    compressedFileUrl
                };
                if (null == pluginData ? void 0 : pluginData.packageName) data['packageName'] = pluginData.packageName;
                api.request({
                    url: "pm:".concat(isUpgrade ? 'update' : 'add'),
                    method: 'post',
                    data
                });
                onClose(true);
            }
        };
    };
    const useValuesFromProps = (options)=>useRequest(()=>Promise.resolve({
                data: {
                    compressedFileUrl: pluginData.compressedFileUrl
                }
            }), options);
    const useCancel = ()=>({
            run () {
                onClose();
            }
        });
    const schema = useMemo(()=>({
            type: 'object',
            properties: {
                [uid()]: {
                    'x-decorator': 'Form',
                    'x-component': 'div',
                    type: 'void',
                    'x-decorator-props': {
                        useValues: '{{ useValuesFromProps }}'
                    },
                    properties: {
                        compressedFileUrl: {
                            type: 'string',
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            required: true
                        },
                        footer: {
                            type: 'void',
                            'x-component': 'ActionBar',
                            'x-component-props': {
                                layout: 'one-column',
                                style: {
                                    justifyContent: 'right'
                                }
                            },
                            properties: {
                                cancel: {
                                    title: 'Cancel',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        useAction: '{{ useCancel }}'
                                    }
                                },
                                submit: {
                                    title: '{{t("Submit")}}',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        type: 'primary',
                                        htmlType: 'submit',
                                        useAction: '{{ useSaveValues }}'
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }), []);
    return /*#__PURE__*/ jsx(SchemaComponent, {
        scope: {
            useCancel,
            useValuesFromProps,
            useSaveValues
        },
        schema: schema
    });
};
export { PluginUrlForm };
