/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as k } from "../../chunks/runtime.js";
import { css as L, html as a } from "lit";
import z from "@arcgis/core/rest/query/support/AttachmentInfo.js";
import S from "@arcgis/core/rest/support/AttachmentQuery.js";
import I from "@arcgis/core/core/Collection.js";
import b from "@arcgis/core/core/Error.js";
import { LitElement as _, nothing as p, safeClassMap as w, safeStyleMap as $ } from "@arcgis/lumina";
import { classes as A } from "@arcgis/toolkit/dom";
import { g as u } from "../../chunks/feature-utils.js";
import { watch as C } from "@arcgis/core/core/reactiveUtils.js";
function v(i) {
  const e = i.toLowerCase();
  return e === "image/bmp" || e === "image/emf" || e === "image/exif" || e === "image/gif" || e === "image/x-icon" || e === "image/jpeg" || e === "image/png" || e === "image/tiff" || e === "image/x-wmf";
}
function x(i) {
  return i ? i === "text/plain" ? "file-text" : i === "application/pdf" ? "file-pdf" : i === "text/csv" ? "file-csv" : i === "application/gpx+xml" ? "file-gpx" : i === "application/x-dwf" ? "file-cad" : i === "application/postscript" || i === "application/json" || i === "text/xml" || i === "model/vrml" ? "file-code" : i === "application/x-zip-compressed" || i === "application/x-7z-compressed" || i === "application/x-gzip" || i === "application/x-tar" || i === "application/x-gtar" || i === "application/x-bzip2" || i === "application/gzip" || i === "application/x-compress" || i === "application/x-apple-diskimage" || i === "application/x-rar-compressed" || i === "application/zip" ? "file-zip" : i.includes("image/") ? "file-image" : i.includes("audio/") ? "file-sound" : i.includes("video/") ? "file-video" : i.includes("msexcel") || i.includes("ms-excel") || i.includes("spreadsheetml") ? "file-excel" : i.includes("msword") || i.includes("ms-word") || i.includes("wordprocessingml") ? "file-word" : i.includes("powerpoint") || i.includes("presentationml") ? "file-report" : "file" : "file";
}
const P = L`.item-link{display:flex;flex-grow:1}.item-link img{max-width:100%;max-height:100%;object-fit:contain}.item-link--list{align-items:center;justify-content:center;width:64px;max-height:64px;overflow:hidden;border-radius:var(--calcite-corner-radius-sm)}.item-link--list img{border-radius:var(--calcite-corner-radius-sm)}.item-link--list-icon{border:var(--calcite-border-width-sm) solid var(--calcite-color-border-3)}.item-link--list calcite-icon{margin:var(--calcite-spacing-lg)}.item-link--preview{align-items:center;justify-content:center}.item-link--preview calcite-icon{margin-block:var(--calcite-spacing-lg)}.item-description{text-align:center;word-wrap:break-word;word-break:break-word}calcite-link{--calcite-link-text-color: var(--calcite-color-text-3);padding-inline:var(--calcite-spacing-sm);word-wrap:break-word;word-break:break-word}calcite-card{width:100%}`, j = I.ofType(z);
class R extends _ {
  constructor() {
    super(...arguments), this._attachmentInfos = new j(), this._attachmentLayer = null, this._supportsImageOrientation = window?.CSS?.supports("image-orientation", "from-image"), this.getAttachmentsPromise = null, this.hideSection = !1, this.headingLevel = 2;
  }
  static {
    this.properties = { effectiveDisplayType: 16, getAttachmentsPromise: 16, hideSection: 16, supportsResizeAttachments: 16, attachmentKeywords: 0, attachmentTypes: 0, description: 1, displayType: 1, graphic: 0, heading: 1, messages: 0, orderByFields: 0, headingLevel: 9 };
  }
  static {
    this.styles = P;
  }
  get _supportsTypeWildcard() {
    const { graphic: e } = this;
    if (!e)
      return !1;
    const t = u(e);
    return t?.loaded && "capabilities" in t && t.capabilities && "attachment" in t.capabilities && t.capabilities.attachment && "supportsResize" in t.capabilities.attachment && t.capabilities.attachment.supportsTypeWildcard || !1;
  }
  get effectiveDisplayType() {
    const { displayType: e } = this;
    return !e || e === "auto" ? this.supportsResizeAttachments ? "preview" : "list" : e;
  }
  get supportsResizeAttachments() {
    const { graphic: e } = this;
    if (!e)
      return !1;
    const t = u(e);
    return t?.loaded && "capabilities" in t && t.capabilities && "attachment" in t.capabilities && t.capabilities.attachment && "supportsResize" in t.capabilities.attachment && t.capabilities.attachment.supportsResize || !1;
  }
  loaded() {
    this.manager.onLifecycle(() => [
      C(() => this.graphic, () => this.graphicChanged(), { initial: !0 })
    ]);
  }
  async getAttachments() {
    const { _attachmentLayer: e, _attachmentInfos: t, orderByFields: l, attachmentTypes: r, attachmentKeywords: s, _supportsTypeWildcard: g } = this;
    if (!e || typeof e.queryAttachments != "function")
      throw new b("invalid-layer", "getAttachments(): A valid layer is required.");
    const o = this.graphic?.getObjectId();
    if (typeof o != "number")
      throw new b("invalid-object-id", "getAttachments(): Numeric object id is required");
    const m = l?.map((c) => `${c.field} ${c.order === "descending" ? "DESC" : "ASC"}`), f = new S({
      objectIds: [o],
      returnMetadata: !0,
      orderByFields: m,
      attachmentTypes: g ? r?.filter(Boolean).map((c) => `${c}/*`) : void 0,
      keywords: s
    }), n = [], h = e.queryAttachments(f).then((c) => c[o] || n).catch(() => n);
    this.getAttachmentsPromise = h;
    const d = await h;
    t.destroyAll(), d.length && t.addMany(d), this.getAttachmentsPromise = null;
  }
  setAttachmentLayer() {
    const { graphic: e } = this, t = u(e);
    this._attachmentLayer = t ? t.type === "scene" && t.associatedLayer != null ? t.associatedLayer : t : null;
  }
  graphicChanged() {
    this.graphic && (this.setAttachmentLayer(), this.getAttachments().catch(() => this._attachmentInfos.destroyAll()));
  }
  getCSSTransform(e) {
    const { orientationInfo: t } = e;
    return !this._supportsImageOrientation && t ? [
      t.rotation ? `rotate(${t.rotation}deg)` : "",
      t.mirrored ? "scaleX(-1)" : ""
    ].join(" ") : "";
  }
  render() {
    return a`<div class="root" .hidden=${!(this.getAttachmentsPromise || this._attachmentInfos.length > 0)}><arcgis-feature-element-info .heading=${this.heading} .description=${this.description} .headingLevel=${this.headingLevel}></arcgis-feature-element-info>${this.renderAttachments()}</div>`;
  }
  renderAttachmentsList(e) {
    return a`<calcite-list .loading=${!!this.getAttachmentsPromise} .label=${this.messages?.componentLabel ?? ""} interaction-mode=static selection-mode=none>${e.toArray().filter(Boolean).map((t) => this.renderAttachmentListItem(t))}</calcite-list>`;
  }
  renderAttachmentsPreview(e) {
    return a`<calcite-card-group .label=${this.messages?.componentLabel ?? ""}>${this.getAttachmentsPromise ? a`<calcite-card loading></calcite-card>` : e.toArray().filter(Boolean).map((t) => this.renderAttachmentPreviewItem(t))}</calcite-card-group>`;
  }
  renderAttachments() {
    const { effectiveDisplayType: e, _attachmentInfos: t } = this;
    return e === "preview" ? this.renderAttachmentsPreview(t) : this.renderAttachmentsList(t);
  }
  renderAttachmentListItem(e) {
    const { supportsResizeAttachments: t } = this, { contentType: l, name: r, url: s, size: g } = e, o = 64, m = r || this.messages?.noTitle, f = x(l), n = this.getCSSTransform(e), h = n ? { transform: n, "image-orientation": "none" } : {}, d = t && v(l), c = a`<a class=${w(A("item-link", "item-link--list", {
      "item-link--list-icon": !d
    }))} slot=content-start href=${s ?? void 0 ?? p} rel=noreferrer target=_blank>${d ? a`<img alt=${r ?? p} src=${`${s}${s?.includes("?") ? "&" : "?"}w=${o}&s=${g}`} style=${$(h)} title=${r ?? p}>` : a`<calcite-icon .icon=${f}></calcite-icon>`}</a>`, y = a`<calcite-link slot=content .href=${s ?? void 0} rel=noreferrer target=_blank>${m}</calcite-link>`;
    return a`<calcite-list-item .label=${m}>${c}${y}</calcite-list-item>`;
  }
  renderAttachmentPreviewItem(e) {
    const { supportsResizeAttachments: t } = this, { contentType: l, name: r, url: s, size: g } = e, o = 225, m = r || this.messages?.noTitle, f = x(l), n = this.getCSSTransform(e), h = n ? { transform: n, "image-orientation": "none" } : {}, d = a`<a class=${w(A("item-link", "item-link--preview"))} slot=thumbnail href=${s ?? void 0 ?? p} rel=noreferrer target=_blank>${t && v(l) ? a`<img alt=${r ?? p} src=${`${s}${s?.includes("?") ? "&" : "?"}w=${o}&s=${g}`} style=${$(h)} title=${r ?? p}>` : a`<calcite-icon scale=l .icon=${f}></calcite-icon>`}</a>`;
    return a`<calcite-card .label=${m}>${d}<span class="item-description" slot=description>${m}</span></calcite-card>`;
  }
}
k("arcgis-feature-attachments", R);
export {
  R as ArcgisFeatureAttachments
};
