import { c as f } from "../../chunks/runtime.js";
import { css as k, html as s } from "lit";
import { hasSameOrigin as v } from "@arcgis/toolkit/url";
import { watch as g, whenOnce as $ } from "@arcgis/core/core/reactiveUtils.js";
import { LitElement as b, noShadowRoot as x, safeClassMap as c, nothing as w } from "@arcgis/lumina";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.34/esri/copyright.txt for details.
v4.34.7 */
const _ = k`@layer{.arcgis-print__exported-file{display:flex;align-items:center;justify-content:space-between}.arcgis-print__exported-file-link-title,.arcgis-print__exported-file-link-description{word-wrap:break-word;word-break:break-all;overflow:hidden;text-overflow:ellipsis;display:-webkit-box;-webkit-line-clamp:2;line-clamp:2;-webkit-box-orient:vertical}}`, d = "arcgis-print", m = {
  exportedFile: `${d}__exported-file`,
  exportedFileLinkTitle: `${d}__exported-file-link-title`,
  exportedFileLinkDescription: `${d}__exported-file-link-description`
};
function I(r) {
  const { state: e, extension: t } = r;
  switch (e) {
    case "pending":
      return "spinner";
    case "error":
      return "exclamation-mark-circle";
    default:
      return t?.toLowerCase()?.includes("pdf") ? "file-pdf" : "file";
  }
}
function u(r, e) {
  return !r || r === "all" || !!(e && r.includes(e));
}
class E extends b {
  constructor() {
    super(...arguments), this._handleLinkClick = (e) => {
      if (!e || e.state !== "ready" || !e.url)
        return;
      const t = e.url, i = document.createElement("a");
      i.target = "_blank", i.href = t, i.rel = "noreferrer", i.download = e.formattedName ?? "";
      {
        i.click();
        return;
      }
    };
  }
  static {
    this.properties = { saveAsButtonCallback: 0, allowedFormatsForSaving: 1, exportedLink: 0, viewModel: 0, messages: 0 };
  }
  static {
    this.styles = _;
  }
  static {
    this.shadowRootOptions = x;
  }
  loaded() {
    this.manager.onLifecycle(() => [
      g(() => this.exportedLink.state, () => this.requestUpdate())
    ]);
  }
  render() {
    const { exportedLink: e } = this, t = e.formattedName ?? "";
    return s`<calcite-list-item .label=${t}>${this._renderExportedLinkItemContent(e)}${this._renderExportedLinkItemIcon(e)}${this._renderExportedLinkItemAction(e)}</calcite-list-item>${this._renderExportedLinkItemPopover(e)}`;
  }
  _renderExportedLinkItemPopover(e) {
    const t = this.messages, i = e.formattedName ?? "", { state: n } = e, a = this.saveAsButtonCallback && n !== "error" && u(this.allowedFormatsForSaving, e.extension);
    return n !== "pending" && a ? s`<calcite-popover auto-close label=Popover offset-distance=1 overlay-positioning=fixed pointer-disabled .referenceElement=${`${i}-popover-button`} scale=s><calcite-list label>${e.portalItem ? s`<calcite-list-item .label=${t.openItem} @calciteListItemSelect=${() => {
      const l = e.portalItem;
      if (!l?.itemPageUrl)
        return;
      const o = document.createElement("a");
      o.target = "_blank", o.href = l.itemPageUrl, o.rel = "noreferrer", o.click();
    }}><calcite-icon icon=launch2 scale=s slot=content-start></calcite-icon></calcite-list-item>` : s`<calcite-list-item .label=${t.saveAs} @calciteListItemSelect=${async () => {
      this.saveAsButtonCallback && ($(() => e.portalItem).then((l) => {
        l.itemPageUrl && this.requestUpdate();
      }), await this.saveAsButtonCallback(e));
    }}><calcite-icon icon=save scale=s slot=content-start></calcite-icon></calcite-list-item>`}<calcite-list-item label=Delete @calciteListItemSelect=${() => this.viewModel.exportedLinks.remove(e)}><calcite-icon icon=trash scale=s slot=content-start></calcite-icon></calcite-list-item></calcite-list></calcite-popover>` : null;
  }
  _renderExportedLinkItemContent(e) {
    const t = this.messages, { url: i, state: n, error: a } = e, l = e.formattedName ?? "", p = v(i || null, document.location.href), h = n === "error" ? a?.name === "print-task:cim-symbol-unsupported" ? t.exportWebMapCIMError : t.exportWebMapError : null;
    return s`<div class=${c(m.exportedFile)} slot=content @click=${() => this._handleLinkClick(e)}><div><div class=${c(m.exportedFileLinkTitle)}>${l}</div><div class=${c(m.exportedFileLinkDescription)}>${n === "pending" ? t.generatingExport : h || (p ? t.ready : t.linkReady)}</div></div>${n === "ready" ? s`<calcite-icon .ariaLabel=${`${l}. ${t.linkReady}`} .icon=${p ? "download-to" : "launch"} scale=s slot=content-end></calcite-icon>` : null}</div>`;
  }
  _renderExportedLinkItemIcon(e) {
    return e.state === "pending" ? s`<calcite-loader inline .label=${this.messages.generatingExport} scale=m slot=content-start></calcite-loader>` : s`<calcite-icon .icon=${I(e)} scale=s slot=content-start></calcite-icon>`;
  }
  _renderExportedLinkItemAction(e) {
    const { state: t } = e, i = e.formattedName ?? "", n = this.saveAsButtonCallback && t !== "error" && u(this.allowedFormatsForSaving, e.extension);
    return t !== "pending" ? s`<calcite-action .icon=${n ? "ellipsis" : "x"} id=${(n ? `${i}-popover-button` : void 0) ?? w} @click=${n ? void 0 : () => {
      this.viewModel.exportedLinks.remove(e);
    }} slot=actions-end .text=${n ? "options" : "remove"}></calcite-action>` : null;
  }
}
f("arcgis-print-exported-link", E);
export {
  E as PrintExportedLink
};
