import { Component, h, Method, Prop } from "@stencil/core";
import { Locale } from "isoly";
import { Token } from "authly";
import { paymentOptionCardIs, } from "../model";
import { paymentDetailsAdd } from "../payment/options/PaymentDetails";
export class IntergiroCardDetails {
  constructor() {
    this.created = false;
  }
  async getPayment(order, apiKey) {
    var _a, _b;
    let result;
    if (this.option && order.items && order.currency) {
      const response = await this.cardInput.submit((_b = (_a = order.payment) === null || _a === void 0 ? void 0 : _a.card) !== null && _b !== void 0 ? _b : undefined);
      if (!this.created)
        this.created = Token.is(response);
      result = Token.is(response) ? { type: "card", card: response, client: this.getClientData() } : undefined;
    }
    return result;
  }
  async confirmPayment(order, payment, apiKey, error) {
    let result;
    if (order.items && order.currency) {
      const response = await this.cardInput.submit("card" in payment ? payment.card : undefined, error);
      result = Token.is(response) ? { type: "card", card: response, client: this.getClientData() } : undefined;
    }
    return result;
  }
  getClientData() {
    return {
      browser: {
        colorDepth: screen.colorDepth,
        resolution: [screen.width, screen.height],
        java: navigator.javaEnabled(),
        javascript: true,
        locale: Locale.is(navigator.language) ? navigator.language : "en-GB",
        timezone: new Date().getTimezoneOffset(),
        parent: window.location.origin,
      },
    };
  }
  render() {
    return this.option ? (h("intergiro-card-input", { "api-key": this.option.apiKey, ref: (element) => (this.cardInput = element), cosmetic: this.cosmetic })) : ([]);
  }
  static get is() { return "ig-card-details"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "option": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "PaymentOptionCard",
        "resolved": "Card | undefined",
        "references": {
          "PaymentOptionCard": {
            "location": "import",
            "path": "../model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "created": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "created",
      "reflect": false,
      "defaultValue": "false"
    },
    "cosmetic": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "cosmetic",
      "reflect": false
    }
  }; }
  static get methods() { return {
    "getPayment": {
      "complexType": {
        "signature": "(order: Order | Omit<Order.Creatable, \"payment\">, apiKey: string) => Promise<PaymentCardCreatable | undefined>",
        "parameters": [{
            "tags": [],
            "text": ""
          }, {
            "tags": [],
            "text": ""
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "PaymentCardCreatable": {
            "location": "import",
            "path": "../model"
          },
          "Order": {
            "location": "import",
            "path": "../model"
          },
          "Omit": {
            "location": "global"
          }
        },
        "return": "Promise<PaymentCardCreatable | undefined>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    },
    "confirmPayment": {
      "complexType": {
        "signature": "(order: Order | Partial<Order>, payment: PaymentCreatable, apiKey: string, error?: Error | undefined) => Promise<PaymentCardCreatable | undefined>",
        "parameters": [{
            "tags": [],
            "text": ""
          }, {
            "tags": [],
            "text": ""
          }, {
            "tags": [],
            "text": ""
          }, {
            "tags": [],
            "text": ""
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "PaymentCardCreatable": {
            "location": "import",
            "path": "../model"
          },
          "Order": {
            "location": "import",
            "path": "../model"
          },
          "Partial": {
            "location": "global"
          },
          "PaymentCreatable": {
            "location": "import",
            "path": "../model"
          },
          "Error": {
            "location": "import",
            "path": "gracely"
          }
        },
        "return": "Promise<PaymentCardCreatable | undefined>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    }
  }; }
}
paymentDetailsAdd("card", (option, cosmetic) => {
  const result = {
    type: "card",
    name: "Card",
    brand: option.service,
    component: (h("ig-card-details", { option: paymentOptionCardIs(option) ? option : undefined, cosmetic: cosmetic, ref: (e) => (result.element = e) })),
  };
  return result;
});
