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 useCurrentUserValues = (options)=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCurrentUserContext)();
    return (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)(()=>Promise.resolve(ctx.data), options);
};
const useSaveCurrentUserValues = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCurrentUserContext)();
    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 () {
            var _ctx_data;
            const values = await form.submit();
            setVisible(false);
            await api.resource('users').updateProfile({
                values
            });
            ctx.mutate({
                data: {
                    ...null == ctx ? void 0 : null === (_ctx_data = ctx.data) || void 0 === _ctx_data ? void 0 : _ctx_data.data,
                    ...values
                }
            });
        }
    };
};
const schema = {
    type: 'object',
    properties: {
        [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
            'x-decorator': 'Form',
            'x-decorator-props': {
                useValues: '{{ useCurrentUserValues }}'
            },
            'x-component': 'Action.Drawer',
            type: 'void',
            title: '{{t("Edit profile")}}',
            properties: {
                nickname: {
                    type: 'string',
                    title: "{{t('Nickname')}}",
                    'x-decorator': 'FormItem',
                    'x-component': 'Input'
                },
                username: {
                    type: 'string',
                    title: '{{t("Username")}}',
                    'x-decorator': 'FormItem',
                    'x-component': 'Input',
                    'x-validator': {
                        username: true
                    },
                    required: true
                },
                email: {
                    type: 'string',
                    title: '{{t("Email")}}',
                    'x-decorator': 'FormItem',
                    'x-component': 'Input',
                    'x-validator': 'email'
                },
                phone: {
                    type: 'string',
                    title: '{{t("Phone")}}',
                    'x-decorator': 'FormItem',
                    'x-component': 'Input',
                    'x-validator': 'phone'
                },
                footer: {
                    'x-component': 'Action.Drawer.Footer',
                    type: 'void',
                    properties: {
                        cancel: {
                            title: 'Cancel',
                            'x-component': 'Action',
                            'x-component-props': {
                                useAction: '{{ useCloseAction }}'
                            }
                        },
                        submit: {
                            title: 'Submit',
                            'x-component': 'Action',
                            'x-component-props': {
                                type: 'primary',
                                useAction: '{{ useSaveCurrentUserValues }}'
                            }
                        }
                    }
                }
            }
        }
    }
};
const useEditProfile = ()=>{
    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: 'profile',
            eventKey: 'EditProfile',
            onClick: ()=>{
                setVisible(true);
                null == ctx || ctx.setVisible(false);
            },
            label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                children: [
                    t('Edit profile'),
                    /*#__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: {
                                    useCurrentUserValues,
                                    useCloseAction,
                                    useSaveCurrentUserValues
                                },
                                schema: schema
                            })
                        })
                    })
                ]
            })
        }), [
        visible
    ]);
};
export { useEditProfile };
