import { defineComponent, ref, openBlock, createBlock, unref, withCtx, createElementVNode, withModifiers, createVNode } from "vue";
import { u as useI18n, b as useAppStore, l as useUserStore, k as useAuthService, _ as _sfc_main$1, D as DxForm_1, f as DxSimpleItem_1, g as DxLabel_1, p as DxRequiredRule_1, r as DxEmailRule_1, v as DxAsyncRule_1, t as DxCompareRule_1, h as DxButtonItem_1, n as notify } from "./index-67606569.mjs";
const _hoisted_1 = ["onSubmit"];
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "Authentication",
  setup(__props) {
    const { t } = useI18n();
    const formRef = ref(null);
    const appStore = useAppStore();
    const userStore = useUserStore();
    const authService = useAuthService();
    const state = ref({
      ...userStore.data,
      new_password: null,
      current_password: null
    });
    const onFormSubmit = async (event) => {
      const validation = await formRef.value.instance.validate().complete;
      if (!validation.isValid) {
        event.preventDefault();
      } else {
        appStore.loading = true;
        try {
          const result = await authService.updatePassword({
            username: state.value.username,
            password: state.value.new_password,
            password_confirmation: state.value.password_confirmation
          });
          if (result) {
            notify(t("validation.unexpectedError"), "danger");
          } else {
            notify(t("message.updateSuccess"), "success");
          }
        } finally {
          appStore.loading = false;
        }
      }
    };
    const passwordComparison = () => {
      return state.value.new_password;
    };
    const validate = async (data) => {
      const result = await authService.validatePassword(data);
      if (result) {
        const message = data.username ? "validation.rules.isEmailUnique" : "validation.rules.invalidCurrentPassword";
        return Promise.reject(t(message));
      }
    };
    const isValidEmail = (params) => validate({ username: params.value });
    const isValidPassword = (params) => validate({ password: params.value });
    return (_ctx, _cache) => {
      return openBlock(), createBlock(_sfc_main$1, {
        title: unref(t)("updateCredentials")
      }, {
        default: withCtx(() => [
          createElementVNode("form", {
            autocomplete: "off",
            novalidate: "",
            onSubmit: withModifiers(onFormSubmit, ["prevent"])
          }, [
            createVNode(unref(DxForm_1), {
              ref_key: "formRef",
              ref: formRef,
              "label-mode": "static",
              "form-data": state.value,
              "show-colon-after-label": false
            }, {
              default: withCtx(() => [
                createVNode(unref(DxSimpleItem_1), {
                  "data-field": "username",
                  "editor-type": "dxTextBox"
                }, {
                  default: withCtx(() => [
                    createVNode(unref(DxLabel_1), {
                      text: unref(t)("attributes.email")
                    }, null, 8, ["text"]),
                    createVNode(unref(DxRequiredRule_1)),
                    createVNode(unref(DxEmailRule_1)),
                    createVNode(unref(DxAsyncRule_1), {
                      "validation-callback": isValidEmail,
                      message: unref(t)("validation.rules.isEmailUnique")
                    }, null, 8, ["message"])
                  ]),
                  _: 1
                }),
                createVNode(unref(DxSimpleItem_1), {
                  "data-field": "api_token",
                  "editor-type": "dxTextBox",
                  "editor-options": {
                    readOnly: true
                  }
                }, {
                  default: withCtx(() => [
                    createVNode(unref(DxLabel_1), {
                      text: unref(t)("attributes.token")
                    }, null, 8, ["text"])
                  ]),
                  _: 1
                }),
                createVNode(unref(DxSimpleItem_1), {
                  "data-field": "current_password",
                  "editor-type": "dxTextBox",
                  "editor-options": {
                    mode: "password"
                  }
                }, {
                  default: withCtx(() => [
                    createVNode(unref(DxLabel_1), {
                      text: unref(t)("attributes.current_password")
                    }, null, 8, ["text"]),
                    createVNode(unref(DxRequiredRule_1)),
                    createVNode(unref(DxAsyncRule_1), { "validation-callback": isValidPassword })
                  ]),
                  _: 1
                }),
                createVNode(unref(DxSimpleItem_1), {
                  "data-field": "new_password",
                  "editor-type": "dxTextBox",
                  "editor-options": {
                    mode: "password"
                  }
                }, {
                  default: withCtx(() => [
                    createVNode(unref(DxLabel_1), {
                      text: unref(t)("attributes.new_password")
                    }, null, 8, ["text"]),
                    createVNode(unref(DxRequiredRule_1))
                  ]),
                  _: 1
                }),
                createVNode(unref(DxSimpleItem_1), {
                  "data-field": "password_confirmation",
                  "editor-type": "dxTextBox",
                  "editor-options": {
                    mode: "password"
                  }
                }, {
                  default: withCtx(() => [
                    createVNode(unref(DxLabel_1), {
                      text: unref(t)("attributes.password_confirmation")
                    }, null, 8, ["text"]),
                    createVNode(unref(DxRequiredRule_1)),
                    createVNode(unref(DxCompareRule_1), {
                      "comparison-target": passwordComparison,
                      message: unref(t)("validation.invalidPasswordConfirmation")
                    }, null, 8, ["message"])
                  ]),
                  _: 1
                }),
                createVNode(unref(DxButtonItem_1), {
                  "horizontal-alignment": "center",
                  "button-options": {
                    text: unref(t)("update"),
                    type: "success",
                    useSubmitBehavior: true
                  }
                }, null, 8, ["button-options"])
              ]),
              _: 1
            }, 8, ["form-data"])
          ], 40, _hoisted_1)
        ]),
        _: 1
      }, 8, ["title"]);
    };
  }
});
export {
  _sfc_main as default
};
