import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__ from "../../../schema-settings/LinkageRules/type.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__ from "../../common/utils/uitls.mjs";
const validateJSON = {
    validator: "{{(value, rule)=> {\n    if (!value) {\n      return '';\n    }\n    try {\n      const val = JSON.parse(value);\n      if(!isNaN(val)) {\n        return false;\n      }\n      return true;\n    } catch(error) {\n      console.error(error);\n      return false;\n    }\n  }}}",
    message: '{{t("Invalid JSON format")}}'
};
const requestSettingsSchema = {
    type: 'object',
    properties: {
        url: {
            type: 'string',
            title: '{{t("Request URL")}}',
            required: true,
            'x-decorator': 'FormItem',
            'x-component': 'Input'
        },
        method: {
            type: 'string',
            title: '{{t("Request method")}}',
            'x-decorator': 'FormItem',
            'x-component': 'Select',
            default: 'POST',
            enum: [
                {
                    label: 'POST',
                    value: 'POST'
                },
                {
                    label: 'GET',
                    value: 'GET'
                },
                {
                    label: 'PUT',
                    value: 'PUT'
                },
                {
                    label: 'PATCH',
                    value: 'PATCH'
                },
                {
                    label: 'DELETE',
                    value: 'DELETE'
                }
            ]
        },
        headers: {
            type: 'string',
            title: '{{t("Request headers")}}',
            'x-decorator': 'FormItem',
            'x-component': 'Input.TextArea',
            'x-validator': validateJSON
        },
        params: {
            type: 'string',
            title: '{{t("Request query parameters")}}',
            'x-decorator': 'FormItem',
            'x-component': 'Input.TextArea',
            'x-validator': validateJSON
        },
        data: {
            type: 'string',
            title: '{{t("Request body")}}',
            'x-decorator': 'FormItem',
            'x-component': 'Input.TextArea',
            'x-validator': validateJSON
        }
    }
};
const linkageAction = async (param)=>{
    let { operator, field, condition, variables, localVariables } = param;
    var _field_stateOfLinkageRules, _field_stateOfLinkageRules1;
    const disableResult = (null == field ? void 0 : null === (_field_stateOfLinkageRules = field.stateOfLinkageRules) || void 0 === _field_stateOfLinkageRules ? void 0 : _field_stateOfLinkageRules.disabled) || [
        false
    ];
    const displayResult = (null == field ? void 0 : null === (_field_stateOfLinkageRules1 = field.stateOfLinkageRules) || void 0 === _field_stateOfLinkageRules1 ? void 0 : _field_stateOfLinkageRules1.display) || [
        'visible'
    ];
    switch(operator){
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Visible:
            if (await (0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            })) {
                displayResult.push(operator);
                field.data = field.data || {};
                field.data.hidden = false;
            }
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                display: displayResult
            };
            field.display = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.last)(displayResult);
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Hidden:
            if (await (0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            })) {
                field.data = field.data || {};
                field.data.hidden = true;
            } else {
                field.data = field.data || {};
                field.data.hidden = false;
            }
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Disabled:
            if (await (0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            })) disableResult.push(true);
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                disabled: disableResult
            };
            field.disabled = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.last)(disableResult);
            field.componentProps['disabled'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.last)(disableResult);
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Active:
            if (await (0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            })) disableResult.push(false);
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                disabled: disableResult
            };
            field.disabled = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.last)(disableResult);
            field.componentProps['disabled'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.last)(disableResult);
            break;
        default:
            return null;
    }
};
const afterSuccessSchema = (t)=>({
        type: 'object',
        title: t('After successful submission'),
        properties: {
            successMessage: {
                title: '{{t("Popup message")}}',
                'x-decorator': 'FormItem',
                'x-component': 'Input.TextArea',
                'x-component-props': {}
            },
            popupClose: {
                title: '{{t("Popup close method")}}',
                enum: [
                    {
                        label: '{{t("Automatic close")}}',
                        value: false
                    },
                    {
                        label: '{{t("Manually close")}}',
                        value: true
                    }
                ],
                'x-decorator': 'FormItem',
                'x-component': 'Radio.Group',
                'x-component-props': {}
            },
            manualClose: {
                title: '{{t("Message clear metchod")}}',
                enum: [
                    {
                        label: '{{t("Automatic close")}}',
                        value: false
                    },
                    {
                        label: '{{t("Manually close")}}',
                        value: true
                    }
                ],
                'x-decorator': 'FormItem',
                'x-component': 'Radio.Group',
                'x-component-props': {}
            },
            redirecting: {
                title: '{{t("Then")}}',
                enum: [
                    {
                        label: '{{t("Stay on current page")}}',
                        value: false
                    },
                    {
                        label: '{{t("Redirect to")}}',
                        value: true
                    }
                ],
                'x-decorator': 'FormItem',
                'x-component': 'Radio.Group',
                'x-component-props': {},
                'x-reactions': {
                    target: 'redirectTo',
                    fulfill: {
                        state: {
                            visible: '{{!!$self.value}}'
                        }
                    }
                }
            },
            redirectTo: {
                title: '{{t("Link")}}',
                'x-decorator': 'FormItem',
                'x-component': 'Input',
                'x-component-props': {}
            }
        }
    });
export { afterSuccessSchema, linkageAction, requestSettingsSchema };
