import { b as buildAssetsURL } from '../../handlers/renderer.mjs';
import { useSSRContext, reactive, ref, toRef, watch, resolveComponent, mergeProps, withCtx, createVNode, openBlock, createBlock, Fragment, renderList, toDisplayString, computed, h, defineComponent } from 'vue';
import { _ as _export_sfc, d as defineNuxtComponent, p as IdentityProviderList, V as IdentityProviderIcon, y as RealmList, g as useAuthStore, W as useValidationTranslator, R as REALM_MASTER_NAME, f as useRoute, n as navigateTo, X as useAPIClient, e as useRuntimeConfig } from '../server.mjs';
import { isClientError } from '@hapic/oauth2';
import { FormSubmit, FormInput } from '@vue-layout/form-controls';
import useVuelidate from '@vuelidate/core';
import { required, minLength, maxLength } from '@vuelidate/validators';
import { useToast } from 'bootstrap-vue-next';
import { ssrRenderAttrs, ssrRenderComponent, ssrRenderList, ssrRenderAttr, ssrInterpolate } from 'vue/server-renderer';
import 'vue-bundle-renderer/runtime';
import 'h3';
import 'devalue';
import '../../nitro/node-server.mjs';
import 'node-fetch-native/polyfill';
import 'node:http';
import 'node:https';
import 'destr';
import 'ofetch';
import 'unenv/runtime/fetch/index';
import 'hookable';
import 'scule';
import 'klona';
import 'defu';
import 'ohash';
import 'ufo';
import 'unstorage';
import 'radix3';
import 'node:fs';
import 'node:url';
import 'pathe';
import 'http-graceful-shutdown';
import 'unctx';
import 'vue-router';
import '@ebec/http';
import 'hapic';
import 'rapiq';
import '@hapic/vault';
import '@vue-layout/navigation';
import 'change-case';
import '@vue-layout/list-controls/core';
import 'smob';
import '@vue-layout/list-controls';
import '@vue-layout/pagination';
import '@ucast/mongo2js';
import 'ilingo';
import 'nanoid';
import '@unhead/ssr';
import 'unhead';
import '@unhead/shared';
import '@vue-layout/preset-bootstrap-v5';
import '@vue-layout/preset-font-awesome';
import '@vue-layout/countdown';
import '@vue-layout/timeago';
import 'browser-storage-adapter';
import 'cookie-es';

const __nuxt_component_0 = /* @__PURE__ */ defineNuxtComponent({
  props: {
    listRef: {
      type: Object
    },
    entity: {
      type: Object,
      required: true
    },
    modelValue: {
      type: String
    }
  },
  emits: ["update:modelValue"],
  setup(props, { emit }) {
    const modelValue = toRef(props, "modelValue");
    toRef(props, "listRef");
    const isMaster = computed(() => props.entity.name === REALM_MASTER_NAME);
    const canCheck = computed(() => !(isMaster.value && !modelValue.value) && modelValue.value !== props.entity.id);
    return () => {
      if (canCheck.value) {
        return h("button", {
          class: {
            "btn btn-xs btn-primary": canCheck.value,
            "btn btn-xs btn-danger": !canCheck.value
          },
          onClick($event) {
            $event.preventDefault();
            emit("update:modelValue", props.entity.id);
          }
        }, [
          h("i", {
            class: {
              "fa fa-check": canCheck.value,
              "fa fa-times": !canCheck.value
            }
          })
        ]);
      }
      return h("button", { class: "btn btn-xs btn-disabled btn-success", disabled: true }, [
        h("i", {
          class: "fa-solid fa-check"
        })
      ]);
    };
  }
}, "$BvqHu6TDsR");
const Login = "" + buildAssetsURL("Login.bb43092d.svg");
const LoginSVG = /* @__PURE__ */ defineComponent({
  props: {
    width: {
      type: [Number, String],
      default: 750
    },
    height: {
      type: [Number, String],
      default: 500
    }
  },
  setup(props) {
    return () => h("img", {
      src: Login,
      width: props.width,
      height: props.height,
      style: {
        maxWidth: "100%"
      }
    });
  }
});
const _sfc_main = /* @__PURE__ */ defineNuxtComponent({
  components: {
    LoginSVG,
    IdentityProviderList,
    IdentityProviderIcon,
    RealmList,
    RealmSelectAction: __nuxt_component_0,
    FormSubmit,
    FormInput
  },
  setup() {
    const toast = useToast();
    const form = reactive({
      name: "",
      password: "",
      realm_id: ""
    });
    const vuelidate = useVuelidate({
      name: {
        required,
        minLength: minLength(3),
        maxLength: maxLength(255)
      },
      password: {
        required,
        minLength: minLength(3),
        maxLength: maxLength(255)
      },
      realm_id: {}
    }, form);
    const store = useAuthStore();
    const runtimeConfig = /* @__PURE__ */ useRuntimeConfig();
    const busy = ref(false);
    const realmId = toRef(form, "realm_id");
    const identityProviderQuery = {
      filters: {
        realm_id: realmId.value || ""
      }
    };
    const identityProviderRef = ref(null);
    watch(realmId, async (val, oldVal) => {
      if (val !== oldVal) {
        if (identityProviderRef.value) {
          identityProviderQuery.filters.realm_id = realmId.value;
          identityProviderRef.value.load();
        }
      }
    });
    const submit = async () => {
      try {
        await store.login({
          name: form.name,
          password: form.password,
          realmId: form.realm_id
        });
        const route = useRoute();
        const { redirect, ...query } = route.query;
        navigateTo({
          path: redirect || "/",
          query
        });
      } catch (e) {
        if (isClientError(e) && toast) {
          toast.warning({ body: e.message }, { pos: "top-center" });
        }
      }
    };
    Promise.resolve().then(store.logout);
    const buildIdentityProviderURL = (id) => {
      const apiClient = useAPIClient();
      return apiClient.identityProvider.getAuthorizeUri(
        runtimeConfig.public.apiUrl,
        id
      );
    };
    return {
      vuelidate,
      translator: useValidationTranslator(),
      form,
      submit,
      busy,
      identityProviderQuery,
      identityProviderRef,
      buildIdentityProviderURL
    };
  }
}, "$V4hxAv6YfE");
function _sfc_ssrRender(_ctx, _push, _parent, _attrs, $props, $setup, $data, $options) {
  const _component_LoginSVG = resolveComponent("LoginSVG");
  const _component_FormInput = resolveComponent("FormInput");
  const _component_FormSubmit = resolveComponent("FormSubmit");
  const _component_IdentityProviderList = resolveComponent("IdentityProviderList");
  const _component_IdentityProviderIcon = resolveComponent("IdentityProviderIcon");
  const _component_RealmList = resolveComponent("RealmList");
  const _component_RealmSelectAction = __nuxt_component_0;
  _push(`<div${ssrRenderAttrs(mergeProps({ class: "container" }, _attrs))} data-v-bd0b1065><h4 data-v-bd0b1065><i class="fa-solid fa-arrow-right-to-bracket pe-2" data-v-bd0b1065></i> Login </h4><div class="text-center" data-v-bd0b1065>`);
  _push(ssrRenderComponent(_component_LoginSVG, { height: 320 }, null, _parent));
  _push(`</div><form data-v-bd0b1065><div class="row" data-v-bd0b1065><div class="col-8" data-v-bd0b1065>`);
  _push(ssrRenderComponent(_component_FormInput, {
    modelValue: _ctx.form.name,
    "onUpdate:modelValue": ($event) => _ctx.form.name = $event,
    "validation-result": _ctx.vuelidate.name,
    "validation-translator": _ctx.translator,
    label: true,
    "label-content": "Name"
  }, null, _parent));
  _push(ssrRenderComponent(_component_FormInput, {
    modelValue: _ctx.form.password,
    "onUpdate:modelValue": ($event) => _ctx.form.password = $event,
    type: "password",
    "validation-result": _ctx.vuelidate.password,
    "validation-translator": _ctx.translator,
    label: true,
    "label-content": "Password"
  }, null, _parent));
  _push(ssrRenderComponent(_component_FormSubmit, {
    modelValue: _ctx.busy,
    "onUpdate:modelValue": ($event) => _ctx.busy = $event,
    "validation-result": _ctx.vuelidate,
    "create-text": "Login",
    "create-button-class": { value: "btn btn-sm btn-dark btn-block", presets: { bootstrap: false } },
    "create-icon-class": "fa-solid fa-right-to-bracket",
    submit: _ctx.submit
  }, null, _parent));
  _push(`<hr data-v-bd0b1065>`);
  _push(ssrRenderComponent(_component_IdentityProviderList, {
    ref: "identityProviderRef",
    query: _ctx.identityProviderQuery,
    "footer-pagination": false
  }, {
    header: withCtx((_, _push2, _parent2, _scopeId) => {
      if (_push2) {
        _push2(`<h6 data-v-bd0b1065${_scopeId}>IdentityProvider</h6>`);
      } else {
        return [
          createVNode("h6", null, "IdentityProvider")
        ];
      }
    }),
    body: withCtx((props, _push2, _parent2, _scopeId) => {
      if (_push2) {
        _push2(`<div class="d-flex flex-row" data-v-bd0b1065${_scopeId}><!--[-->`);
        ssrRenderList(props.data, (item, key) => {
          _push2(`<div data-v-bd0b1065${_scopeId}><a${ssrRenderAttr("href", _ctx.buildIdentityProviderURL(item.id))} class="btn btn-dark btn-xs p-2 me-1 identity-provider-box bg-dark" data-v-bd0b1065${_scopeId}><div class="d-flex flex-column" data-v-bd0b1065${_scopeId}><div class="text-center mb-1" data-v-bd0b1065${_scopeId}>`);
          _push2(ssrRenderComponent(_component_IdentityProviderIcon, {
            class: "fa-2x",
            entity: item
          }, null, _parent2, _scopeId));
          _push2(`</div><div data-v-bd0b1065${_scopeId}>${ssrInterpolate(item.name)}</div></div></a></div>`);
        });
        _push2(`<!--]--></div>`);
      } else {
        return [
          createVNode("div", { class: "d-flex flex-row" }, [
            (openBlock(true), createBlock(Fragment, null, renderList(props.data, (item, key) => {
              return openBlock(), createBlock("div", { key }, [
                createVNode("a", {
                  href: _ctx.buildIdentityProviderURL(item.id),
                  class: "btn btn-dark btn-xs p-2 me-1 identity-provider-box bg-dark"
                }, [
                  createVNode("div", { class: "d-flex flex-column" }, [
                    createVNode("div", { class: "text-center mb-1" }, [
                      createVNode(_component_IdentityProviderIcon, {
                        class: "fa-2x",
                        entity: item
                      }, null, 8, ["entity"])
                    ]),
                    createVNode("div", null, toDisplayString(item.name), 1)
                  ])
                ], 8, ["href"])
              ]);
            }), 128))
          ])
        ];
      }
    }),
    _: 1
  }, _parent));
  _push(`</div><div class="col-4" data-v-bd0b1065>`);
  _push(ssrRenderComponent(_component_RealmList, null, {
    itemActions: withCtx((props, _push2, _parent2, _scopeId) => {
      if (_push2) {
        _push2(ssrRenderComponent(_component_RealmSelectAction, {
          modelValue: _ctx.form.realm_id,
          "onUpdate:modelValue": ($event) => _ctx.form.realm_id = $event,
          "list-ref": _ctx.identityProviderRef,
          entity: props.data
        }, null, _parent2, _scopeId));
      } else {
        return [
          createVNode(_component_RealmSelectAction, {
            modelValue: _ctx.form.realm_id,
            "onUpdate:modelValue": ($event) => _ctx.form.realm_id = $event,
            "list-ref": _ctx.identityProviderRef,
            entity: props.data
          }, null, 8, ["modelValue", "onUpdate:modelValue", "list-ref", "entity"])
        ];
      }
    }),
    _: 1
  }, _parent));
  _push(`</div></div></form></div>`);
}
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
  const ssrContext = useSSRContext();
  (ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("pages/login.vue");
  return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
const login = /* @__PURE__ */ _export_sfc(_sfc_main, [["ssrRender", _sfc_ssrRender], ["__scopeId", "data-v-bd0b1065"]]);

export { login as default };
//# sourceMappingURL=login-52767ea8.mjs.map
