/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as a } from "../../chunks/runtime.js";
import { LitElement as o } from "@arcgis/lumina";
import { useContextConsumer as n } from "@arcgis/lumina/context";
import { c as l } from "../../chunks/deployAppContexts.js";
import { restGetUser as s } from "@esri/solution-common";
import { css as r, html as u } from "lit";
import { A as c } from "../../chunks/interfaces.js";
const m = r`*[tabindex]:focus-visible{outline:2px solid var(--calcite-color-brand);outline-offset:2px}.avatar-container{align-items:center;display:flex;flex-direction:row;padding-top:var(--calcite-spacing-lg)}.full-name{margin-inline-start:0px;font-size:var(--calcite-font-size--1);font-weight:var(--calcite-font-weight-medium);color:var(--calcite-navigation-user-full-name-text-color, var(--calcite-color-text-1))}.text-container{margin-block-start:.125rem;display:flex;flex-direction:column;padding-inline:1rem;text-align:start}.username{font-size:var(--calcite-font-size--1);color:var(--calcite-navigation-user-name-text-color, var(--calcite-color-text-2))}`;
class f extends o {
  constructor() {
    super(), this.lookupProfile = !1, this.username = "", this._configContextConsumer = n({
      context: l,
      subscribe: !0
    }), this.listenOn(window, "sessionRefreshed", (t) => {
      this._updateThumbnailToken(t.detail);
    });
  }
  static {
    this.properties = { fullname: 1, lookupProfile: 5, thumbnailUrl: 1, username: 1 };
  }
  static {
    this.styles = m;
  }
  loaded() {
    this.lookupProfile && this._lookupOwnerProfile(this.username);
  }
  willUpdate(t) {
    t.has("username") && this.lookupProfile && this._lookupOwnerProfile(this.username);
  }
  render() {
    return u`<div class="avatar-container"><calcite-avatar scale=m .username=${this.username} .thumbnail=${this.thumbnailUrl ?? void 0} .fullName=${this.fullname ?? void 0}></calcite-avatar><div class="text-container"><span class="full-name" tabindex=0>${this.fullname ?? ""}</span><span class="username" tabindex=0>${this.username}</span></div></div>`;
  }
  async _lookupOwnerProfile(t) {
    if (this.fullname = void 0, this.thumbnailUrl = "", this.lookupProfile && this._configContextConsumer.value) {
      const { userSession: i } = this._configContextConsumer.value;
      if (i)
        try {
          const e = await s({
            authentication: i,
            username: t
          });
          e && (e.fullName && (this.fullname = e.fullName), e.thumbnail && (this.thumbnailUrl = i.portal + "/community/users/" + e.id + "/info/" + e.thumbnail + "?token=" + i.token));
        } catch {
          console.log("user not found");
        }
    }
  }
  _updateThumbnailToken(t) {
    this.thumbnailUrl && this.thumbnailUrl !== "" && (this.thumbnailUrl = c(this.thumbnailUrl, t));
  }
}
a("solution-user-avatar", f);
export {
  f as SolutionUserAvatar
};
