'use strict';

const index$1 = require('./index-df541290.js');
const index$5 = require('./index-0a8af373.js');
const index$7 = require('./index-b8688617.js');
const index$6 = require('./index-ad93d157.js');
const index$3 = require('./index-fe328b96.js');
require('./index-5e992066.js');
const index$2 = require('./index-c12b8888.js');
const index = require('./index-b7493032.js');
const index$4 = require('./index-3e5f1089.js');

const sv = {
  Deselect: "Avmarkera",
  Select: "Markera",
  Link: "Länk",
  "Send receipt as email to customer": "Sänd kvitto via email till kunden",
  "Open receipt as pdf": "Öppna kvitto i pdf-format",
  "Sending receipt...": "Skickar kvitto...",
  "Successfully sent email": "E-post skickat",
  "Failed to send email": "Misslyckades med att skicka e-post",
  unknown: "okänd",
};

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

const styleCss = ".sc-ig-orders-table-row-h{display:table-row;border-top:1px solid rgba(var(--smoothly-medium-color), 0);border-bottom:1px solid rgba(var(--smoothly-medium-color), 0)}.sc-ig-orders-table-row-h:hover{-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow);border-top-color:rgb(var(--smoothly-medium-color));border-bottom-color:rgb(var(--smoothly-medium-color))}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>.openable.sc-ig-orders-table-row{padding:.5em .3em;width:-webkit-max-content;width:-moz-max-content;width:max-content;min-width:3em;display:inline-block}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>.openable.sc-ig-orders-table-row:hover{background-color:rgb(var(--smoothly-dark-shade));cursor:pointer}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>.openable.opened.sc-ig-orders-table-row{border:solid 1px rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));border-bottom:3px solid rgb(var(--smoothly-dark-shade));border-radius:0 0 2px 2px;margin-bottom:-2px}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>smoothly-checkbox.sc-ig-orders-table-row{display:inline-block;vertical-align:-40%;margin-left:.3em}smoothly-icon.sc-ig-orders-table-row{display:inline-block;margin-left:.4em}smoothly-icon.receipt.sc-ig-orders-table-row{vertical-align:-24%;cursor:pointer}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row:last-child>div.sc-ig-orders-table-row{width:100%;max-width:20em;display:-ms-flexbox;display:flex;-ms-flex-line-pack:stretch;align-content:stretch;-ms-flex-wrap:nowrap;flex-wrap:nowrap}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>div.sc-ig-orders-table-row>ig-status.sc-ig-orders-table-row{margin-right:.2em;padding-right:0.5em;-ms-flex-negative:5;flex-shrink:5;width:100%}";

const IntergiroOrdersTableRow = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.orderSelected = index$1.createEvent(this, "orderSelected", 7);
    this.columnClicked = index$1.createEvent(this, "columnClicked", 7);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.display = false;
  }
  async componentWillLoad() {
    this.t = create(this.element);
    await this.getAudience();
  }
  onChecked(event) {
    this.orderSelected.emit({ order: this.order, checked: !this.selected });
    event.stopPropagation();
    event.preventDefault();
  }
  async getAudience() {
    var _a, _b, _c;
    if (index$2.Connection.key) {
      const unpacked = await ((_a = index$3.Verifier.create()) === null || _a === void 0 ? void 0 : _a.verify(index$2.Connection.key));
      this.audience = unpacked
        ? typeof unpacked.aud == "string"
          ? unpacked.aud
          : (_c = (_b = unpacked.aud) === null || _b === void 0 ? void 0 : _b.reduce((p, c) => (p == "customer" ? p : c), "")) !== null && _c !== void 0 ? _c : ""
        : "";
    }
    else
      this.audience = "";
  }
  handleColumnClick(column) {
    this.columnClicked.emit({ id: this.order.id, column });
  }
  async sendReceiptEmail() {
    this.notice.emit(index$4.Notice.execute(this.t("Sending receipt..."), async () => {
      const response = await index$2.Connection.get(`order/${this.order.id}/receipt?email=true`);
      return !index$5.Error.is(response) ? [true, this.t("Successfully sent email")] : [false, this.t("Failed to send email")];
    }));
  }
  render() {
    var _a, _b, _c, _d;
    const t = this.t;
    const contact = index$2.Contact.is(this.order.customer) ? this.order.customer : undefined;
    return [
      !this.display ? (index$1.h("td", null, index$1.h("smoothly-checkbox", { selected: this.selected }))) : (""),
      index$1.h("td", null, index$1.h("div", { class: "openable" + (this.opened == "number" ? " opened" : ""), onClick: () => this.handleColumnClick("number") }, this.order.number)),
      !this.display ? (index$1.h("td", null, index$1.h("div", { class: "openable" + (this.opened == "contact" ? " opened" : ""), onClick: () => this.handleColumnClick("contact") }, (_a = index$2.Contact.getLabel(contact)) !== null && _a !== void 0 ? _a : (index$2.Payment.Defer.is(this.order.payment) ? this.order.payment.contact : t("unknown"))))) : (""),
      index$1.h("td", null, index$1.h("smoothly-display-amount", { class: "openable" + (this.opened == "items" ? " opened" : ""), onClick: () => this.handleColumnClick("items"), amount: index$2.Item.amount(this.order.items), currency: this.order.currency })),
      this.receipt ? (index$1.h("td", null, this.order.payment.type == "card" && [
        index$1.h("a", { href: index$2.Connection.baseUrl +
            "order/" +
            this.order.id +
            "/receipt?merchant=" +
            index$2.Connection.key +
            "&accept=application%2Fpdf", target: "_blank" }, index$1.h("smoothly-icon", { class: "receipt", name: "receipt", size: "small", toolTip: t("Open receipt as pdf") })),
        this.audience != "customer" && (contact === null || contact === void 0 ? void 0 : contact.email) && (index$1.h("smoothly-icon", { class: "receipt", name: "mail", size: "small", toolTip: t("Send receipt as email to customer"), onClick: () => this.sendReceiptEmail() })),
      ])) : (""),
      index$1.h("td", null, index$1.h("div", { class: "openable" + (this.opened == "payment" ? " opened" : ""), onClick: () => this.handleColumnClick("payment") }, index$2.Payment.Card.is(this.order.payment)
        ? index$6.dist.format(this.order.payment.iin + "******" + this.order.payment.last4, "card-number")
        : index$2.Payment.Defer.is(this.order.payment) && (contact === null || contact === void 0 ? void 0 : contact.email)
          ? this.order.payment.contact
          : index$2.Payment.Defer.is(this.order.payment) && this.order.payment.link
            ? this.order.payment.link.substring(0, 8) +
              "..." +
              this.order.payment.link.slice(this.order.payment.link.length - 16)
            : "")),
      index$1.h("td", null, index$1.h("smoothly-display-date-time", { class: "openable" + (this.opened == "created" ? " opened" : ""), onClick: () => this.handleColumnClick("created"), datetime: this.order.created ? index$7.dist.DateTime.localize(index$7.dist.DateTime.parse(this.order.created)) : "" })),
      index$1.h("td", null, index$1.h("div", null, this.order.status
        ? Array.isArray(this.order.status)
          ? (_c = (_b = this.order.status) === null || _b === void 0 ? void 0 : _b.map((status) => index$1.h("ig-status", { status: status }))) !== null && _c !== void 0 ? _c : []
          : (_d = Object.entries(this.order.status).map(status => index$2.Status.is(status[0]) && index$1.h("ig-status", { status: status[0] }))) !== null && _d !== void 0 ? _d : []
        : [])),
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroOrdersTableRow.style = styleCss;

exports.IntergiroOrdersTableRow = IntergiroOrdersTableRow;
