'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-0b1e2673.js');
require('./App-1647a125.js');
require('./_commonjsHelpers-937cd5b3.js');
const index$2 = require('./index-1a9e1965.js');
require('./index-451ca284.js');
const index$3 = require('./index-ab186017.js');
const index = require('./index-36cf3884.js');
const index$4 = require('./index-061d0ccd.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 index.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:4.5em;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:4.5em}";

const PayfuncSubscriptionDetails = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.opened = "";
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  cancel() {
    this.notice.emit(index$2.Notice.execute(this.t("Cancelling subscription..."), async () => {
      const response = await index$3.Connection.delete(`account/${this.account.id}/subscription/${this.subscription.id}`);
      let result;
      if (!index$4.dist.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(index$2.Notice.execute(this.t("Updating subscription..."), async () => {
      const response = await index$3.Connection.put(`account/${this.account.id}/subscription/${this.subscription.id}`, this.subscription);
      let result;
      if (!index$4.dist.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 = (index$1.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 = (index$1.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 = index$1.h("payfunc-schedule", { schedule: this.subscription.schedule });
        break;
      case "end":
        result = [
          index$1.h("label", null, this.t("End")),
          index$1.h("section", null, index$1.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") }), index$1.h("smoothly-icon", { onClick: () => this.cancel(), name: "trash", color: "default", fill: "solid", toolTip: this.t("Cancel subscription") })),
        ];
        break;
      case "start":
        result = [
          index$1.h("label", null, this.t("Start")),
          index$1.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 = (index$1.h("payfunc-future-due-dates", { dates: index$3.Schedule.calculateDueDates(this.account.schedule, 5, this.subscription.start, this.subscription.end) }));
        break;
      default:
        return "";
    }
    return index$1.h("td", { colSpan: 8 }, result);
  }
  get element() { return index$1.getElement(this); }
};
PayfuncSubscriptionDetails.style = styleCss;

exports.payfunc_subscription_details = PayfuncSubscriptionDetails;
