var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
  if (!privateMap.has(receiver)) {
    throw new TypeError("attempted to get private field on non-instance");
  }
  return privateMap.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
  if (!privateMap.has(receiver)) {
    throw new TypeError("attempted to set private field on non-instance");
  }
  privateMap.set(receiver, value);
  return value;
};
var _flags;
import { Component, Event, h, Listen, Prop, State } from "@stencil/core";
import { get as getFlag, parse as parseFlags } from "flagly";
import { Error } from "gracely";
import "smoothly";
import { Item, MerchantKeyUnpack, openClient, Order, } from "./model";
export class PayfuncCheckout {
  constructor() {
    this.processing = false;
    _flags.set(this, void 0);
    this.buttonDisabled = false;
  }
  get flags() {
    var _a;
    if (!__classPrivateFieldGet(this, _flags) && this.features)
      __classPrivateFieldSet(this, _flags, parseFlags(this.features));
    return (_a = __classPrivateFieldGet(this, _flags)) !== null && _a !== void 0 ? _a : {};
  }
  async componentWillLoad() {
    var _a, _b;
    this.merchant = await MerchantKeyUnpack(this.apiKey, "public");
    this.checkoutClient = await openClient((_b = (_a = this.merchant) === null || _a === void 0 ? void 0 : _a.iss) !== null && _b !== void 0 ? _b : "", this.apiKey, "order", "contact");
    if (!getFlag(this.flags, "contact"))
      this.contactChanged.emit(this.parsedOrder.customer);
  }
  get parsedOrder() {
    var _a;
    if (!this.orderValue) {
      let order = { id: Order.generateId() };
      if (this.order)
        order = typeof this.order == "string" ? JSON.parse(this.order) : this.order;
      if (this.items)
        order = Object.assign(Object.assign({}, order), { items: typeof this.items == "string" ? JSON.parse(this.items) : this.items });
      if (this.currency)
        order = Object.assign(Object.assign({}, order), { currency: this.currency });
      if (this.number)
        order = Object.assign(Object.assign({}, order), { number: this.number });
      if (this.customer)
        order = Object.assign(Object.assign({}, order), { customer: typeof order.customer == "string"
            ? order.customer
            : Object.assign(Object.assign({}, order.customer), (typeof this.customer == "string" ? JSON.parse(this.customer) : this.customer)) });
      if (this.client)
        order = Object.assign(Object.assign({}, order), { client: this.client });
      order = Object.assign(Object.assign({}, order), { category: (_a = this.category) !== null && _a !== void 0 ? _a : "purchase" });
      this.orderValue = order;
    }
    return this.orderValue;
  }
  async onContactChanged(event) {
    var _a, _b;
    this.parsedOrder.customer = event.detail || undefined;
    if (this.merchant) {
      const paymentOptions = await this.checkoutClient.contact.getPaymentOptions(this.parsedOrder.currency, Item.amount(this.parsedOrder.items), (_b = (_a = event.detail) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : "default");
      if (!Error.is(paymentOptions)) {
        event.preventDefault();
        this.paymentOptions = paymentOptions.map(option => { var _a; return option.type == "card" && ((_a = this.merchant) === null || _a === void 0 ? void 0 : _a.card) ? Object.assign(Object.assign({}, option), { apiKey: this.apiKey }) : option; });
      }
    }
  }
  getCustomCssVariables() {
    var _a, _b, _c, _d, _e, _f;
    const result = {};
    const cosmetic = typeof this.cosmetic == "string" ? JSON.parse(this.cosmetic) : this.cosmetic;
    const temp = {
      "--smoothly-tertiary-color": (_a = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.header) === null || _a === void 0 ? void 0 : _a.background,
      "--smoothly-primary-contrast": (_b = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.header) === null || _b === void 0 ? void 0 : _b.color,
      "--smoothly-font-family": cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.fontFamily,
      "--smoothly-default-contrast": (_c = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.text) === null || _c === void 0 ? void 0 : _c.color,
      "--smoothly-color-contrast": (_d = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.text) === null || _d === void 0 ? void 0 : _d.color,
      "--smoothly-success-color": (_e = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.submit) === null || _e === void 0 ? void 0 : _e.background,
      "--smoothly-success-contrast": (_f = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.submit) === null || _f === void 0 ? void 0 : _f.color,
      "--smoothly-danger-color": cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.dangerColor,
    };
    Object.entries(temp).forEach(([key, value]) => {
      value && (result[key] = value);
    });
    return result;
  }
  render() {
    return (h("smoothly-notifier", null,
      getFlag(this.flags, "contact") ? (h("ig-contact", { style: this.getCustomCssVariables(), "api-key": this.apiKey, contact: typeof this.parsedOrder.customer != "string" ? this.parsedOrder.customer : undefined })) : ([]),
      this.paymentOptions ? (h("ig-checkout-payment", { client: this.checkoutClient, options: this.paymentOptions, order: this.parsedOrder, merchant: this.merchant, "api-key": this.apiKey, style: this.getCustomCssVariables(), flags: this.flags, cosmetic: !this.cosmetic
          ? undefined
          : typeof this.cosmetic == "string"
            ? this.cosmetic
            : JSON.stringify(this.cosmetic) })) : ([])));
  }
  static get is() { return "payfunc-checkout"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "apiKey": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "api-key",
      "reflect": false
    },
    "cosmetic": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "| string\n\t\t| {\n\t\t\t\theader?: { background?: string; color?: string }\n\t\t\t\ttext?: { background?: string; color?: string }\n\t\t\t\tsubmit?: { background?: string; color?: string }\n\t\t\t\tborder?: { color: string }\n\t\t\t\tfontFamily?: string\n\t\t\t\tdangerColor?: string\n\t\t  }",
        "resolved": "string | undefined | { header?: { background?: string | undefined; color?: string | undefined; } | undefined; text?: { background?: string | undefined; color?: string | undefined; } | undefined; submit?: { background?: string | undefined; color?: string | undefined; } | undefined; border?: { color: string; } | undefined; fontFamily?: string | undefined; dangerColor?: string | undefined; }",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "cosmetic",
      "reflect": false
    },
    "order": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string | Order | Omit<Order, \"payment\">",
        "resolved": "Order | string | undefined | { number?: string | undefined; created: string; id: string; client?: string | undefined; customer?: string | Contact | undefined; items: number | Item | Item[]; currency: Currency; subscription?: string | undefined; event?: Event[] | undefined; status?: Status[] | StatusList | undefined; theme?: string | undefined; meta?: any; callback?: string | undefined; language?: Language | undefined; category?: \"purchase\" | \"withdrawal\" | undefined; }",
        "references": {
          "Order": {
            "location": "import",
            "path": "./model"
          },
          "Omit": {
            "location": "global"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "order",
      "reflect": false
    },
    "processing": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "processing",
      "reflect": true,
      "defaultValue": "false"
    },
    "items": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "string | number | Item | Item[]",
        "resolved": "Item & { status?: Status[] | undefined; } | Item[] | number | string | undefined",
        "references": {
          "Item": {
            "location": "import",
            "path": "./model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "items",
      "reflect": false
    },
    "currency": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "Currency",
        "resolved": "Currency | undefined",
        "references": {
          "Currency": {
            "location": "import",
            "path": "isoly"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "currency",
      "reflect": false
    },
    "number": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "number",
      "reflect": false
    },
    "customer": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string | Partial<Contact>",
        "resolved": "string | undefined | { type?: \"organization\" | \"person\" | undefined; identityNumber?: string | undefined; id?: string | undefined; number?: string | undefined; name?: string | Name | undefined; address?: Address | Addresses | undefined; email?: string | EmailAddresses | undefined; phone?: string | PhoneNumbers | undefined; }",
        "references": {
          "Partial": {
            "location": "global"
          },
          "Contact": {
            "location": "import",
            "path": "./model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "customer",
      "reflect": false
    },
    "client": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "Identifier",
        "resolved": "string | undefined",
        "references": {
          "Identifier": {
            "location": "import",
            "path": "authly"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "client",
      "reflect": false
    },
    "features": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "features",
      "reflect": false
    },
    "category": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"purchase\" | \"withdrawal\"",
        "resolved": "\"purchase\" | \"withdrawal\" | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "category",
      "reflect": false
    }
  }; }
  static get states() { return {
    "signedOrder": {},
    "buttonDisabled": {},
    "paymentOptions": {},
    "merchant": {}
  }; }
  static get events() { return [{
      "method": "notice",
      "name": "notice",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Notice",
        "resolved": "Notice",
        "references": {
          "Notice": {
            "location": "import",
            "path": "smoothly"
          }
        }
      }
    }, {
      "method": "contactChanged",
      "name": "contactChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get listeners() { return [{
      "name": "contactChanged",
      "method": "onContactChanged",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
_flags = new WeakMap();
