/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as customElement } from "../../chunks/runtime.js";
import { LitElement } from "@arcgis/lumina";
import { useContextConsumer } from "@arcgis/lumina/context";
import { c as configContext } from "../../chunks/deployAppContexts.js";
import { restGetUser } from "@esri/solution-common";
import { css, html } from "lit";
const styles = css`/** @license
 * Copyright 2025 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

.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 SolutionUserAvatar extends LitElement {
  constructor() {
    super(...arguments);
    this.lookupProfile = false;
    this.username = "";
    this._configContextConsumer = useContextConsumer({
      context: configContext,
      subscribe: true
    });
  }
  static {
    this.properties = { fullname: 1, lookupProfile: [5, {}, { type: Boolean }], thumbnailUrl: 1, username: 1 };
  }
  static {
    this.styles = styles;
  }
  loaded() {
    if (this.lookupProfile) {
      this._lookupOwnerProfile(this.username);
    }
  }
  willUpdate(_changedProperties) {
    if (_changedProperties.has("username") && this.lookupProfile) {
      this._lookupOwnerProfile(this.username);
    }
  }
  render() {
    return html`<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">${this.fullname ?? ""}</span><span class="username">${this.username}</span></div></div>`;
  }
  async _lookupOwnerProfile(username) {
    this.fullname = void 0;
    this.thumbnailUrl = void 0;
    if (this.lookupProfile && this._configContextConsumer.value) {
      const { userSession } = this._configContextConsumer.value;
      if (userSession) {
        try {
          const ownerInfo = await restGetUser({
            authentication: userSession,
            username
          });
          if (ownerInfo) {
            if (ownerInfo.fullName) {
              this.fullname = ownerInfo.fullName;
            }
            if (ownerInfo.thumbnail) {
              this.thumbnailUrl = userSession.portal + "/community/users/" + ownerInfo.id + "/info/" + ownerInfo.thumbnail + "?token=" + userSession.token;
            }
          }
        } catch {
          console.log("user not found");
        }
      }
    }
  }
}
customElement("solution-user-avatar", SolutionUserAvatar);
export {
  SolutionUserAvatar
};
