import { h as l } from "vue";
const i = {};
function u(e) {
  return Object.keys(e).reduce((r, t) => (e[t] !== !1 && e[t] !== null && e[t] !== void 0 && (r[t] = e[t]), r), {});
}
const c = {
  name: "InlineSvg",
  inheritAttrs: !1,
  render() {
    return this.svgElSource ? l(
      "svg",
      Object.assign(
        {},
        // source attrs
        this.getSvgAttrs(this.svgElSource),
        // component attrs and listeners
        u(this.$attrs),
        // content
        { innerHTML: this.getSvgContent(this.svgElSource) }
      )
    ) : null;
  },
  props: {
    src: {
      type: String,
      required: !0
    },
    title: {
      type: String
    },
    transformSource: {
      type: Function,
      default: (e) => e
    },
    keepDuringLoading: {
      type: Boolean,
      default: !0
    }
  },
  emits: ["loaded", "unloaded", "error"],
  data() {
    return {
      /** @type {SVGElement} */
      svgElSource: null,
      /** @type {XMLHttpRequest} */
      request: void 0
    };
  },
  watch: {
    src(e) {
      this.getSource(e);
    }
  },
  mounted() {
    this.getSource(this.src);
  },
  methods: {
    getSvgAttrs(e) {
      let r = {};
      const t = e.attributes;
      if (!t)
        return r;
      for (let n = t.length - 1; n >= 0; n--)
        r[t[n].name] = t[n].value;
      return r;
    },
    getSvgContent(e) {
      return e = e.cloneNode(!0), e = this.transformSource(e), this.title && a(e, this.title), e.innerHTML;
    },
    /**
     * Get svgElSource
     * @param {string} src
     */
    getSource(e) {
      i[e] || (i[e] = this.download(e)), this.svgElSource && i[e].getIsPending() && !this.keepDuringLoading && (this.svgElSource = null, this.$emit("unloaded")), i[e].then((r) => {
        this.svgElSource = r, this.$nextTick(() => {
          this.$emit("loaded", this.$el);
        });
      }).catch((r) => {
        this.svgElSource && (this.svgElSource = null, this.$emit("unloaded")), delete i[e], this.$emit("error", r);
      });
    },
    /**
     * Get the contents of the SVG
     * @param {string} url
     * @returns {PromiseWithState<Element>}
     */
    download(e) {
      return g(new Promise((r, t) => {
        const n = new XMLHttpRequest();
        n.open("GET", e, !0), this.request = n, n.onload = () => {
          if (n.status >= 200 && n.status < 400)
            try {
              let o = new DOMParser().parseFromString(n.responseText, "text/xml").getElementsByTagName("svg")[0];
              o ? r(o) : t(new Error('Loaded file is not valid SVG"'));
            } catch (s) {
              t(s);
            }
          else
            t(new Error("Error loading SVG"));
        }, n.onerror = t, n.send();
      }));
    }
  }
};
function a(e, r) {
  const t = e.getElementsByTagName("title");
  if (t.length)
    t[0].textContent = r;
  else {
    const n = document.createElementNS("http://www.w3.org/2000/svg", "title");
    n.textContent = r, e.insertBefore(n, e.firstChild);
  }
}
function g(e) {
  if (e.getIsPending) return e;
  let r = !0, t = e.then(
    (n) => (r = !1, n),
    (n) => {
      throw r = !1, n;
    }
  );
  return t.getIsPending = function() {
    return r;
  }, t;
}
export {
  c as I
};
//# sourceMappingURL=index-D6bxWkZ1.js.map
