import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { a as apiFetch } from './fetch.js';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { d as defineCustomElement$c } from './sc-alert2.js';
import { d as defineCustomElement$b } from './sc-button2.js';
import { d as defineCustomElement$a } from './sc-card2.js';
import { d as defineCustomElement$9 } from './sc-dashboard-module2.js';
import { d as defineCustomElement$8 } from './sc-flex2.js';
import { d as defineCustomElement$7 } from './sc-format-bytes2.js';
import { d as defineCustomElement$6 } from './sc-icon2.js';
import { d as defineCustomElement$5 } from './sc-spinner2.js';
import { d as defineCustomElement$4 } from './sc-stacked-list2.js';
import { d as defineCustomElement$3 } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$2 } from './sc-tag2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scDownloadsListCss = ":host{display:block}.purchase{display:flex;flex-direction:column;gap:var(--sc-spacing-large)}.single-download .single-download__preview{display:flex;align-items:center;justify-content:center;background:var(--sc-color-gray-200);border-radius:var(--sc-border-radius-small);height:4rem;min-width:4rem;width:4rem}";

const ScDownloadsList$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.renderFileExt = download => {
      var _a, _b, _c, _d, _e, _f, _g, _h, _j;
      if ((_a = download === null || download === void 0 ? void 0 : download.media) === null || _a === void 0 ? void 0 : _a.filename) {
        return (_e = (_d = (_c = (_b = download.media.filename).split) === null || _c === void 0 ? void 0 : _c.call(_b, '.')) === null || _d === void 0 ? void 0 : _d.pop) === null || _e === void 0 ? void 0 : _e.call(_d);
      }
      if (download === null || download === void 0 ? void 0 : download.url) {
        try {
          const url = new URL(download.url);
          if (url.pathname.includes('.')) {
            return (_j = (_h = (_g = (_f = url.pathname).split) === null || _g === void 0 ? void 0 : _g.call(_f, '.')) === null || _h === void 0 ? void 0 : _h.pop) === null || _j === void 0 ? void 0 : _j.call(_h);
          }
        }
        catch (err) {
          console.error(err);
        }
      }
      return h("sc-icon", { name: "file" });
    };
  }
  async downloadItem(download) {
    var _a, _b;
    if (download === null || download === void 0 ? void 0 : download.url) {
      this.downloadFile(download.url, (_a = download === null || download === void 0 ? void 0 : download.name) !== null && _a !== void 0 ? _a : 'file');
      return;
    }
    const mediaId = (_b = download === null || download === void 0 ? void 0 : download.media) === null || _b === void 0 ? void 0 : _b.id;
    if (!mediaId)
      return;
    try {
      this.busy = mediaId;
      const media = (await apiFetch({
        path: addQueryArgs(`surecart/v1/customers/${this.customerId}/expose/${mediaId}`, {
          expose_for: 60,
        }),
      }));
      if (!(media === null || media === void 0 ? void 0 : media.url)) {
        throw {
          message: __('Could not download the file.', 'surecart'),
        };
      }
      this.downloadFile(media === null || media === void 0 ? void 0 : media.url, media.filename);
    }
    catch (e) {
      console.error(e);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = null;
    }
  }
  downloadFile(path, filename) {
    // Create a new link
    const anchor = document.createElement('a');
    anchor.href = path;
    anchor.download = filename;
    // Append to the DOM
    document.body.appendChild(anchor);
    // Trigger `click` event
    anchor.click();
    // To make this work on Firefox we need to wait
    // a little while before removing it.
    setTimeout(() => {
      document.body.removeChild(anchor);
    }, 0);
  }
  render() {
    const downloads = this.downloads || [];
    return (h("sc-dashboard-module", { class: "purchase", part: "base", heading: __('Downloads', 'surecart') }, h("span", { slot: "heading" }, h("slot", { name: "heading" }, this.heading || __('Downloads', 'surecart'))), h("sc-card", { "no-padding": true }, h("sc-stacked-list", null, downloads.map(download => {
      var _a, _b, _c, _d;
      const media = download === null || download === void 0 ? void 0 : download.media;
      return (h("sc-stacked-list-row", { style: { '--columns': '1' } }, h("sc-flex", { class: "single-download", justifyContent: "flex-start", alignItems: "center" }, h("div", { class: "single-download__preview" }, this.renderFileExt(download)), h("div", null, h("div", null, h("strong", null, (_b = (_a = media === null || media === void 0 ? void 0 : media.filename) !== null && _a !== void 0 ? _a : download === null || download === void 0 ? void 0 : download.name) !== null && _b !== void 0 ? _b : '')), h("sc-flex", { justifyContent: "flex-start", alignItems: "center", style: { gap: '0.5em' } }, (media === null || media === void 0 ? void 0 : media.byte_size) && h("sc-format-bytes", { value: media.byte_size }), !!((_c = media === null || media === void 0 ? void 0 : media.release_json) === null || _c === void 0 ? void 0 : _c.version) && (h("sc-tag", { type: "primary", size: "small", style: {
          '--sc-tag-primary-background-color': '#f3e8ff',
          '--sc-tag-primary-color': '#6b21a8',
        } }, "v", (_d = media === null || media === void 0 ? void 0 : media.release_json) === null || _d === void 0 ? void 0 :
        _d.version))))), h("sc-button", { size: "small", slot: "suffix", onClick: () => this.downloadItem(download), busy: (media === null || media === void 0 ? void 0 : media.id) ? this.busy == (media === null || media === void 0 ? void 0 : media.id) : false, disabled: (media === null || media === void 0 ? void 0 : media.id) ? this.busy == (media === null || media === void 0 ? void 0 : media.id) : false }, __('Download', 'surecart'))));
    })))));
  }
  static get style() { return scDownloadsListCss; }
}, [1, "sc-downloads-list", {
    "downloads": [16],
    "customerId": [1, "customer-id"],
    "heading": [1],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-downloads-list", "sc-alert", "sc-button", "sc-card", "sc-dashboard-module", "sc-flex", "sc-format-bytes", "sc-icon", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-tag"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-downloads-list":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScDownloadsList$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-format-bytes":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScDownloadsList = ScDownloadsList$1;
const defineCustomElement = defineCustomElement$1;

export { ScDownloadsList, defineCustomElement };
