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_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/index.mjs";
const useCloseAction = ()=>{
    const { setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    return {
        async run () {
            setVisible(false);
            form.submit((values)=>{
                console.warn(values);
            });
        }
    };
};
const useSaveCurrentUserValues = ()=>{
    const { setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    return {
        async run () {
            await form.submit();
            await api.resource('auth').changePassword({
                values: form.values
            });
            await form.reset();
            setVisible(false);
        }
    };
};
const schema = {
    type: 'object',
    properties: {
        [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
            'x-decorator': 'Form',
            'x-component': 'Action.Drawer',
            type: 'void',
            title: '{{t("Change password")}}',
            properties: {
                oldPassword: {
                    type: 'string',
                    title: '{{t("Old password")}}',
                    required: true,
                    'x-component': 'Password',
                    'x-decorator': 'FormItem'
                },
                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 = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.DropdownVisibleContext);
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            key: 'password',
            eventKey: 'ChangePassword',
            onClick: ()=>{
                setVisible(true);
                null == ctx || ctx.setVisible(false);
            },
            label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                children: [
                    t('Change password'),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ActionContextProvider, {
                        value: {
                            visible,
                            setVisible
                        },
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                            onClick: (e)=>e.stopPropagation(),
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                                scope: {
                                    useCloseAction,
                                    useSaveCurrentUserValues
                                },
                                schema: schema
                            })
                        })
                    })
                ]
            })
        }), [
        visible
    ]);
};
export { useChangePassword };
