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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../../schema-component/index.mjs";
const NPM_REGISTRY_ADDRESS = 'https://registry.npmjs.org/';
const PluginNpmForm = (param)=>{
    let { onClose, isUpgrade, pluginData } = param;
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const versionList = [];
    const useSaveValues = ()=>{
        const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
        return {
            async run () {
                await form.submit();
                api.request({
                    url: isUpgrade ? 'pm:update' : 'pm:add',
                    method: 'post',
                    data: {
                        ...form.values
                    }
                });
                onClose(true);
            }
        };
    };
    const useValuesFromProps = (options)=>(0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>Promise.resolve({
                data: isUpgrade ? (0, __WEBPACK_EXTERNAL_MODULE_lodash__.pick)(pluginData, [
                    'name',
                    'packageName'
                ]) : {}
            }), options);
    const useCancel = ()=>({
            run () {
                onClose();
            }
        });
    const schema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const id = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)();
        const schema = {
            type: 'object',
            properties: {
                [id]: {
                    'x-decorator': 'Form',
                    'x-component': 'div',
                    type: 'void',
                    'x-decorator-props': {
                        useValues: '{{ useValuesFromProps }}'
                    },
                    properties: {
                        packageName: {
                            type: 'string',
                            title: "{{t('Npm package name')}}",
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            required: true,
                            'x-component-props': {
                                disabled: true
                            }
                        },
                        registry: {
                            type: 'string',
                            title: "{{t('Registry url')}}",
                            default: NPM_REGISTRY_ADDRESS,
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            'x-component-props': {
                                placeholder: 'https://registry.npmjs.org/'
                            }
                        },
                        authToken: {
                            type: 'string',
                            title: "{{t('Npm authToken')}}",
                            'x-decorator': 'FormItem',
                            'x-component': 'Input'
                        },
                        version: {
                            type: 'string',
                            title: "{{t('Version')}}",
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            'x-component-props': {
                                placeholder: (null == pluginData ? void 0 : pluginData.version) || ''
                            }
                        },
                        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 }}'
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
        schema.properties[id].properties.packageName['x-component-props'].disabled = isUpgrade;
        if (!isUpgrade) delete schema.properties[id].properties['version'];
        return schema;
    }, [
        isUpgrade
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
        scope: {
            useCancel,
            useSaveValues,
            versionList,
            useValuesFromProps
        },
        schema: schema
    });
};
PluginNpmForm.displayName = 'PluginNpmForm';
export { NPM_REGISTRY_ADDRESS, PluginNpmForm };
