import { 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, useCurrentUserContext, useRequest } from "../index.mjs";
import { useAPIClient } from "../api-client/index.mjs";
const useCloseAction = ()=>{
    const { setVisible } = useActionContext();
    const form = useForm();
    return {
        async run () {
            setVisible(false);
            form.submit((values)=>{
                console.warn(values);
            });
        }
    };
};
const useCurrentUserValues = (options)=>{
    const ctx = useCurrentUserContext();
    return useRequest(()=>Promise.resolve(ctx.data), options);
};
const useSaveCurrentUserValues = ()=>{
    const ctx = useCurrentUserContext();
    const { setVisible } = useActionContext();
    const form = useForm();
    const api = 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.data,
                    ...values
                }
            });
        }
    };
};
const schema = {
    type: 'object',
    properties: {
        [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 = useContext(DropdownVisibleContext);
    const [visible, setVisible] = useState(false);
    const { t } = useTranslation();
    return useMemo(()=>({
            key: 'profile',
            eventKey: 'EditProfile',
            onClick: ()=>{
                setVisible(true);
                null == ctx || ctx.setVisible(false);
            },
            label: /*#__PURE__*/ jsxs("div", {
                children: [
                    t('Edit profile'),
                    /*#__PURE__*/ jsx(ActionContextProvider, {
                        value: {
                            visible,
                            setVisible
                        },
                        children: /*#__PURE__*/ jsx("div", {
                            onClick: (e)=>e.stopPropagation(),
                            children: /*#__PURE__*/ jsx(SchemaComponent, {
                                scope: {
                                    useCurrentUserValues,
                                    useCloseAction,
                                    useSaveCurrentUserValues
                                },
                                schema: schema
                            })
                        })
                    })
                ]
            })
        }), [
        visible
    ]);
};
export { useEditProfile };
