import { defineComponent, ref, reactive, resolveComponent, openBlock, createElementBlock, createElementVNode, createVNode, unref, withCtx, toDisplayString } from "vue";
import { u as useI18n, i as useRoute, j as useRouter, k as useAuthService, l as useUserStore, D as DxForm_1, m as DxGroupItem_1, o as DxItem_1, g as DxLabel_1, p as DxRequiredRule_1, h as DxButtonItem_1, q as DxButtonOptions_1, P as PROTECTED_ROUTE, n as notify } from "./index-67606569.mjs";
const _hoisted_1 = { class: "row justify-content-center mt-4" };
const _hoisted_2 = { class: "col-lg-4 col-md-5" };
const _hoisted_3 = { class: "card bg-secondary border-0 mb-0" };
const _hoisted_4 = { class: "card-body p-5" };
const _hoisted_5 = { class: "row mt-3" };
const _hoisted_6 = { class: "col-6" };
const _hoisted_7 = { class: "col-6 text-right" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "Login",
  setup(__props) {
    const { t } = useI18n();
    const route = useRoute();
    const router = useRouter();
    const formRef = ref();
    const isLoading = ref(false);
    const redirect = route.query.redirect;
    const username = String(route.query.username || "");
    const state = reactive({ username });
    const authService = useAuthService();
    const userStore = useUserStore();
    const onSignInClick = async () => {
      isLoading.value = true;
      try {
        const validation = formRef.value.instance.validate();
        if (validation.isValid) {
          const user = await authService.signIn(state.username, state.password);
          userStore.start(user);
          if (redirect) {
            router.replace(redirect);
          } else {
            router.replace({ path: PROTECTED_ROUTE });
          }
        }
      } catch (e) {
        const message = e.status ? `validation.passwords.${e.status}` : "validation.invalidLoginAndPassword";
        notify({
          message: t(message),
          type: "error"
        });
      } finally {
        isLoading.value = false;
      }
    };
    return (_ctx, _cache) => {
      const _component_router_link = resolveComponent("router-link");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createElementVNode("div", _hoisted_2, [
          createElementVNode("div", _hoisted_3, [
            createElementVNode("div", _hoisted_4, [
              createVNode(unref(DxForm_1), {
                ref_key: "formRef",
                ref: formRef,
                "form-data": state
              }, {
                default: withCtx(() => [
                  createVNode(unref(DxGroupItem_1), {
                    caption: unref(t)("loginIntoAccount")
                  }, {
                    default: withCtx(() => [
                      createVNode(unref(DxItem_1), {
                        "data-field": "username",
                        "editor-type": "dxTextBox",
                        "editor-options": {
                          mode: "email"
                        }
                      }, {
                        default: withCtx(() => [
                          createVNode(unref(DxLabel_1), {
                            text: unref(t)("attributes.email")
                          }, null, 8, ["text"]),
                          createVNode(unref(DxRequiredRule_1))
                        ]),
                        _: 1
                      }),
                      createVNode(unref(DxItem_1), {
                        "data-field": "password",
                        "editor-type": "dxTextBox",
                        "editor-options": {
                          mode: "password"
                        }
                      }, {
                        default: withCtx(() => [
                          createVNode(unref(DxLabel_1), {
                            text: unref(t)("attributes.password")
                          }, null, 8, ["text"]),
                          createVNode(unref(DxRequiredRule_1))
                        ]),
                        _: 1
                      }),
                      createVNode(unref(DxButtonItem_1), null, {
                        default: withCtx(() => [
                          createVNode(unref(DxButtonOptions_1), {
                            width: "100%",
                            type: "default",
                            disabled: isLoading.value,
                            text: unref(t)("signIn"),
                            "element-attr": { "data-test": "signInButton" },
                            onClick: onSignInClick
                          }, null, 8, ["disabled", "text"])
                        ]),
                        _: 1
                      })
                    ]),
                    _: 1
                  }, 8, ["caption"])
                ]),
                _: 1
              }, 8, ["form-data"])
            ])
          ]),
          createElementVNode("div", _hoisted_5, [
            createElementVNode("div", _hoisted_6, [
              createVNode(_component_router_link, {
                class: "text-light",
                to: { name: "recovery" }
              }, {
                default: withCtx(() => [
                  createElementVNode("small", null, toDisplayString(unref(t)("forgotPassword")), 1)
                ]),
                _: 1
              })
            ]),
            createElementVNode("div", _hoisted_7, [
              createVNode(_component_router_link, {
                class: "text-light",
                to: { name: "signup" }
              }, {
                default: withCtx(() => [
                  createElementVNode("small", null, toDisplayString(unref(t)("createAccount")), 1)
                ]),
                _: 1
              })
            ])
          ])
        ])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
