import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useContext, useMemo, useState } from "react";
import { uid, useForm } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { ActionContextProvider, DropdownVisibleContext, SchemaComponent, useActionContext, useApp, useCurrentUserContext } from "../index.mjs";
import { useAPIClient } from "../api-client/index.mjs";
import VerificationCode from "./VerificationCode.mjs";
const useCloseAction = ()=>{
    const { setVisible } = useActionContext();
    const form = useForm();
    return {
        async run () {
            setVisible(false);
            form.submit((values)=>{
                console.warn(values);
            });
        }
    };
};
const useSaveCurrentUserValues = ()=>{
    var _currentUser_data_data, _currentUser_data;
    const { setVisible } = useActionContext();
    const form = useForm();
    const api = useAPIClient();
    const currentUser = useCurrentUserContext();
    const hideOldPassword = (null == currentUser ? void 0 : null == (_currentUser_data = currentUser.data) ? void 0 : null == (_currentUser_data_data = _currentUser_data.data) ? void 0 : _currentUser_data_data.password) === null;
    return {
        async run () {
            await form.submit();
            await api.resource('auth').changePassword({
                values: form.values
            });
            if (hideOldPassword) currentUser.mutate({
                data: {
                    ...currentUser.data.data,
                    password: ''
                }
            });
            await form.reset();
            setVisible(false);
        }
    };
};
const schema = {
    type: 'object',
    properties: {
        [uid()]: {
            'x-decorator': 'Form',
            'x-component': 'Action.Drawer',
            type: 'void',
            title: '{{t("Change password")}}',
            properties: {
                verifyMethod: {
                    type: 'string',
                    required: true,
                    title: '{{t("Verify method")}}',
                    enum: [
                        {
                            label: '{{t("Use old password")}}',
                            value: 'password'
                        },
                        {
                            label: '{{t("Use verification code")}}',
                            value: 'code'
                        }
                    ],
                    'x-component': 'Radio.Group',
                    'x-decorator': 'FormItem',
                    'x-reactions': [
                        {
                            dependencies: [
                                '.phoneExist',
                                '.oldPasswordExist'
                            ],
                            fulfill: {
                                state: {
                                    hidden: '{{ !($deps[0] && $deps[1] && smsVerifyEnabled) }}',
                                    value: '{{ undefined }}'
                                }
                            }
                        }
                    ]
                },
                phoneExist: {
                    type: 'boolean',
                    default: '{{ !!phoneNumber }}',
                    'x-hidden': true
                },
                phone: {
                    type: 'string',
                    title: '{{t("Phone")}}',
                    default: '{{ phoneNumber }}',
                    'x-component': 'Input',
                    'x-validator': 'phone',
                    'x-decorator': 'FormItem',
                    'x-hidden': true
                },
                code: {
                    type: 'string',
                    title: '{{t("Verification code")}}',
                    description: "{{codeDescription}}",
                    'x-component': 'VerificationCode',
                    'x-component-props': {
                        actionType: 'auth:changePassword',
                        targetFieldName: 'phone'
                    },
                    required: true,
                    'x-decorator': 'FormItem',
                    'x-reactions': [
                        {
                            dependencies: [
                                '.verifyMethod',
                                '.phoneExist',
                                '.phone'
                            ],
                            fulfill: {
                                state: {
                                    hidden: "{{\n                          !$deps[1] ||\n                          !smsVerifyEnabled ||\n                          ($deps[0] !== 'code' && $deps[0] !== undefined) // 有 verifyMethod 但未选 code\n                        }}"
                                }
                            }
                        }
                    ]
                },
                oldPasswordExist: {
                    type: 'boolean',
                    default: '{{ oldPassword }}',
                    'x-hidden': true
                },
                oldPassword: {
                    type: 'string',
                    title: '{{t("Old password")}}',
                    required: true,
                    'x-component': 'Password',
                    'x-decorator': 'FormItem',
                    'x-reactions': [
                        {
                            dependencies: [
                                '.verifyMethod',
                                '.oldPasswordExist'
                            ],
                            fulfill: {
                                state: {
                                    hidden: "{{\n                          !$deps[1] ||\n                          (smsVerifyEnabled && $deps[0] !== 'password' && $deps[0] !== undefined)\n                        }}"
                                }
                            }
                        }
                    ]
                },
                newPassword: {
                    type: 'string',
                    title: '{{t("New password")}}',
                    required: true,
                    'x-component': 'Password',
                    'x-decorator': 'FormItem',
                    'x-component-props': {
                        checkStrength: true,
                        style: {}
                    },
                    'x-reactions': [
                        {
                            dependencies: [
                                '.confirmPassword'
                            ],
                            fulfill: {
                                state: {
                                    selfErrors: '{{$deps[0] && $self.value && $self.value !== $deps[0] ? t("Password mismatch") : ""}}'
                                }
                            }
                        }
                    ]
                },
                confirmPassword: {
                    type: 'string',
                    required: true,
                    title: '{{t("Confirm password")}}',
                    'x-component': 'Password',
                    'x-decorator': 'FormItem',
                    'x-component-props': {
                        checkStrength: true,
                        style: {}
                    },
                    'x-reactions': [
                        {
                            dependencies: [
                                '.newPassword'
                            ],
                            fulfill: {
                                state: {
                                    selfErrors: '{{$deps[0] && $self.value && $self.value !== $deps[0] ? t("Password mismatch") : ""}}'
                                }
                            }
                        }
                    ]
                },
                footer: {
                    'x-component': 'Action.Drawer.Footer',
                    type: 'void',
                    properties: {
                        cancel: {
                            title: '{{t("Cancel")}}',
                            'x-component': 'Action',
                            'x-component-props': {
                                useAction: '{{ useCloseAction }}'
                            }
                        },
                        submit: {
                            title: '{{t("Submit")}}',
                            'x-component': 'Action',
                            'x-component-props': {
                                type: 'primary',
                                useAction: '{{ useSaveCurrentUserValues }}'
                            }
                        }
                    }
                }
            }
        }
    }
};
const useChangePassword = ()=>{
    var _currentUser_data_data, _currentUser_data, _currentUser_data_data1, _currentUser_data1;
    const ctx = useContext(DropdownVisibleContext);
    const [visible, setVisible] = useState(false);
    const { t } = useTranslation();
    const pm = useApp().pluginManager;
    const otp = pm.get('@tachybase/plugin-otp');
    const sms = pm.get('@tachybase/plugin-auth-sms');
    const smsVerifyEnabled = !!otp && !!sms;
    const currentUser = useCurrentUserContext();
    const oldPassword = (null == currentUser ? void 0 : null == (_currentUser_data = currentUser.data) ? void 0 : null == (_currentUser_data_data = _currentUser_data.data) ? void 0 : _currentUser_data_data.password) !== null;
    const phoneNumber = null == currentUser ? void 0 : null == (_currentUser_data1 = currentUser.data) ? void 0 : null == (_currentUser_data_data1 = _currentUser_data1.data) ? void 0 : _currentUser_data_data1.phone;
    const codeDescription = phoneNumber ? t('Send verification code to phone: {{phoneNumber}}', {
        phoneNumber
    }) : t('Please fill in your mobile phone number in your personal information first');
    return useMemo(()=>({
            key: 'password',
            eventKey: 'ChangePassword',
            onClick: ()=>{
                setVisible(true);
                null == ctx || ctx.setVisible(false);
            },
            label: /*#__PURE__*/ jsxs(Fragment, {
                children: [
                    t('Change password'),
                    /*#__PURE__*/ jsx(ActionContextProvider, {
                        value: {
                            visible,
                            setVisible
                        },
                        children: /*#__PURE__*/ jsx("div", {
                            onClick: (e)=>e.stopPropagation(),
                            children: /*#__PURE__*/ jsx(SchemaComponent, {
                                scope: {
                                    useCloseAction,
                                    useSaveCurrentUserValues,
                                    oldPassword,
                                    codeDescription,
                                    phoneNumber,
                                    smsVerifyEnabled
                                },
                                components: {
                                    VerificationCode: VerificationCode
                                },
                                schema: schema
                            })
                        })
                    })
                ]
            })
        }), [
        visible,
        oldPassword
    ]);
};
export { useChangePassword };
