import {
  getIconLibrary,
  unwatchIcon,
  watchIcon
} from "./chunk.2S2PIDCO.js";
import {
  requestIcon
} from "./chunk.P52GZVKG.js";
import {
  watch
} from "./chunk.X2OQMWGY.js";
import {
  SolidElement,
  customElement
} from "./chunk.H53WQBDK.js";
import {
  __decorateClass
} from "./chunk.S3NI7NKU.js";

// src/components/icon/icon.ts
import { css, html } from "lit";
import { property, state } from "lit/decorators.js";
import { unsafeSVG } from "lit/directives/unsafe-svg.js";
var parser;
var SdIcon = class extends SolidElement {
  constructor() {
    super(...arguments);
    this.svg = "";
    this.label = "";
    this.library = "default";
    this.color = "currentColor";
  }
  connectedCallback() {
    super.connectedCallback();
    watchIcon(this);
  }
  firstUpdated() {
    this.setIcon();
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    unwatchIcon(this);
  }
  onThemeChange() {
    this.setIcon();
  }
  getUrl() {
    const library = getIconLibrary(this.library);
    if (this.name && library) {
      if (library.name === "_internal") {
        return library.resolver(this.name, this);
      }
      return library.resolver(this.name);
    }
    return this.src;
  }
  handleLabelChange() {
    const hasLabel = typeof this.label === "string" && this.label.length > 0;
    if (hasLabel) {
      this.setAttribute("role", "img");
      this.setAttribute("aria-label", this.label);
      this.removeAttribute("aria-hidden");
    } else {
      this.removeAttribute("role");
      this.removeAttribute("aria-label");
      this.setAttribute("aria-hidden", "true");
    }
  }
  async setIcon() {
    var _a;
    const library = getIconLibrary(this.library);
    const url = this.getUrl();
    if (!parser) {
      parser = new DOMParser();
    }
    if (url) {
      try {
        const file = await requestIcon(url);
        if (url !== this.getUrl()) {
        } else if (file.ok) {
          const doc = parser.parseFromString(file.svg, "text/html");
          const svgEl = doc.body.querySelector("svg");
          if (svgEl !== null) {
            (_a = library == null ? void 0 : library.mutator) == null ? void 0 : _a.call(library, svgEl);
            this.svg = svgEl.outerHTML;
            this.emit("sd-load");
          } else {
            this.svg = "";
            this.emit("sd-error");
          }
        } else {
          this.svg = "";
          this.emit("sd-error");
        }
      } catch (e) {
        this.emit("sd-error");
      }
    } else if (this.svg.length > 0) {
      this.svg = "";
    }
  }
  render() {
    return html`<sd-theme-listener></sd-theme-listener>${unsafeSVG(this.svg)}`;
  }
};
SdIcon.styles = [
  ...SolidElement.styles,
  css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */@layer theme,base,components,utilities;
:host{width:1em;height:1em;box-sizing:content-box!important;display:inline-block}svg{display:block;height:100%;width:100%}:host([color=primary]) svg{color:rgba(var(--sd-color-icon-fill-primary,var(--sd-color-primary)))}:host([color=white]) svg{color:rgba(var(--sd-color-text-white))}`
];
__decorateClass([
  state()
], SdIcon.prototype, "svg", 2);
__decorateClass([
  property({ reflect: true })
], SdIcon.prototype, "name", 2);
__decorateClass([
  property()
], SdIcon.prototype, "src", 2);
__decorateClass([
  property()
], SdIcon.prototype, "label", 2);
__decorateClass([
  property({ reflect: true })
], SdIcon.prototype, "library", 2);
__decorateClass([
  property({ reflect: true })
], SdIcon.prototype, "color", 2);
__decorateClass([
  watch("label")
], SdIcon.prototype, "handleLabelChange", 1);
__decorateClass([
  watch(["name", "src", "library"])
], SdIcon.prototype, "setIcon", 1);
SdIcon = __decorateClass([
  customElement("sd-icon")
], SdIcon);

export {
  SdIcon
};
