import { r as registerInstance, j as createEvent, h, g as getElement } from './index-aeac5cad.js';
import './App-418c0118.js';
import './_commonjsHelpers-91fffb88.js';
import { N as Notice } from './index-4920b0b3.js';
import './index-66cd4113.js';
import { C as Connection, b as Schedule } from './index-ec2a4942.js';
import { d as dist } from './index-fdd60c34.js';
import { d as dist$1 } from './index-909faba2.js';

const sv = {
  "Cancelling subscription...": "Avbryter prenumerationen ...",
  "Subscription was cancelled.": "Prenumerationen avbröts.",
  "Failed to cancel subscription.": "Misslyckades med att avbryta prenumerationen.",
  "Updating subscription...": "Uppdaterar prenummeration.",
  "Subscription was updated.": "Prenumerationen uppdaterades.",
  "Failed to update subscription.": "Misslyckades med att uppdatera prenumerationen.",
  "End date": "Slutdatum",
  "Start date": "Startdatum",
  "Cancel subscription": "Avbryt prenumeration",
  ongoing: "pågående",
  Start: "Påbörjar",
  End: "Upphör",
};

function create(language) {
  return dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-payfunc-subscription-details-h{display:table-row}.sc-payfunc-subscription-details-h>td.sc-payfunc-subscription-details{border-top:solid 1px rgb(var(--smoothly-medium-shade));border-bottom:solid 1px rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));padding-top:.5em}payfunc-future-due-dates.sc-payfunc-subscription-details{width:30%;padding:.5em;margin:auto}payfunc-schedule.sc-payfunc-subscription-details{padding:1em;padding-left:4.5em}payfunc-items-list.sc-payfunc-subscription-details{padding:.5em;margin:auto;width:50%}payfunc-items-list.number.sc-payfunc-subscription-details{width:15%}payfunc-display-edit.sc-payfunc-subscription-details{margin-left:1em;margin-bottom:1em;width:10em}.sc-payfunc-subscription-details-h>td.sc-payfunc-subscription-details>section.sc-payfunc-subscription-details{display:flex;flex-direction:row;justify-content:space-between}.sc-payfunc-subscription-details-h>td.sc-payfunc-subscription-details>section.sc-payfunc-subscription-details>smoothly-icon.sc-payfunc-subscription-details{cursor:pointer;margin-right:1em;margin-top:-.5em}label.sc-payfunc-subscription-details{font-weight:bold;margin-left:1em}";

const PayfuncSubscriptionDetails = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.opened = "";
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  cancel() {
    this.notice.emit(Notice.execute(this.t("Cancelling subscription..."), async () => {
      const response = await Connection.delete(`account/${this.account.id}/subscription/${this.subscription.id}`);
      let result;
      if (!dist$1.Error.is(response))
        result = [true, this.t("Subscription was cancelled.")];
      else
        result = [false, this.t("Failed to cancel subscription.")];
      return result;
    }));
  }
  update(value, property) {
    const update = { [property]: value };
    this.subscription = Object.assign(Object.assign({}, this.subscription), update);
    this.notice.emit(Notice.execute(this.t("Updating subscription..."), async () => {
      const response = await Connection.put(`account/${this.account.id}/subscription/${this.subscription.id}`, this.subscription);
      let result;
      if (!dist$1.Error.is(response))
        result = [true, this.t("Subscription was updated.")];
      else
        result = [false, this.t("Failed to update subscription.")];
      return result;
    }));
  }
  render() {
    var _a;
    let result;
    switch (this.opened) {
      case "number":
        result = (h("payfunc-number-details", { number: this.subscription.number, identifier: this.subscription.id, onNumberUpdated: (e) => this.update(e.detail, "number"), name: "subscription" }));
        break;
      case "amount":
        result = (h("payfunc-items-list", { onChanged: (e) => this.update(e.detail, "items"), editable: true, class: typeof this.subscription.items == "number" ? "number" : "", items: this.subscription.items, currency: this.subscription.currency }));
        break;
      case "schedule":
        result = h("payfunc-schedule", { schedule: this.subscription.schedule });
        break;
      case "end":
        result = [
          h("label", null, this.t("End")),
          h("section", null, h("payfunc-display-edit", { deletable: false, name: this.t("End date"), onChanged: (e) => this.update(e.detail, "end"), value: (_a = this.subscription.end) !== null && _a !== void 0 ? _a : this.t("ongoing") }), h("smoothly-icon", { onClick: () => this.cancel(), name: "trash", color: "default", fill: "solid", toolTip: this.t("Cancel subscription") })),
        ];
        break;
      case "start":
        result = [
          h("label", null, this.t("Start")),
          h("payfunc-display-edit", { deletable: false, name: this.t("Start date"), onChanged: (e) => this.update(e.detail, "start"), value: this.subscription.start }),
        ];
        break;
      case "due":
        result = (h("payfunc-future-due-dates", { dates: Schedule.calculateDueDates(this.account.schedule, 5, this.subscription.start, this.subscription.end) }));
        break;
      default:
        return "";
    }
    return h("td", { colSpan: 8 }, result);
  }
  get element() { return getElement(this); }
};
PayfuncSubscriptionDetails.style = styleCss;

export { PayfuncSubscriptionDetails as payfunc_subscription_details };
