import { Component, Event, h, Prop, State, Watch } from '@stencil/core';
import apiFetch from '../../../../functions/fetch';
import { getProcessorData } from '../../../../functions/processor';
import { shouldReloadElement } from './functions';
export class ScOrderStripePaymentElement {
  constructor() {
    /** Available processors */
    this.processors = [];
    /** Payment mode. */
    this.mode = 'live';
    /** The currency code for the payment element. */
    this.currencyCode = 'usd';
  }
  async componentDidLoad() {
    var _a, _b, _c;
    this.paymentIntent = (await apiFetch({
      method: 'POST',
      path: 'surecart/v1/payment_intents',
      data: {
        amount: typeof ((_a = this === null || this === void 0 ? void 0 : this.order) === null || _a === void 0 ? void 0 : _a.amount_due) === 'number' ? this.order.amount_due : 100,
        currency: this.currencyCode,
        processor_type: 'stripe',
        live_mode: this.mode === 'live',
      },
    }));
    this.clientSecret = (_c = (_b = this.paymentIntent.processor_data) === null || _b === void 0 ? void 0 : _b.stripe) === null || _c === void 0 ? void 0 : _c.client_secret;
  }
  async updatePaymentIntent() {
    var _a, _b, _c;
    if (!((_a = this.paymentIntent) === null || _a === void 0 ? void 0 : _a.id)) {
      return;
    }
    this.paymentIntent = (await apiFetch({
      method: 'PATCH',
      path: `surecart/v1/payment_intents/${this.paymentIntent.id}`,
      data: {
        amount: this.order.amount_due,
        currency: this.currencyCode,
        processor_type: 'stripe',
        live_mode: this.mode === 'live',
      },
    }));
    this.clientSecret = (_c = (_b = this.paymentIntent.processor_data) === null || _b === void 0 ? void 0 : _b.stripe) === null || _c === void 0 ? void 0 : _c.client_secret;
  }
  async handleOrderChange() {
    if (!shouldReloadElement(this.paymentIntent, this.order))
      return;
    this.clientSecret = null;
    await this.updatePaymentIntent();
  }
  /**
   * When the payment intent is first set,
   * we need to sync it if we have an order.
   */
  async handlePaymentIntentChange() {
    // check if we should reload the payment intent
    if (shouldReloadElement(this.paymentIntent, this.order)) {
      await this.updatePaymentIntent();
    }
    // update payment intent in form when it's set.
    this.scSetPaymentIntent.emit({ processor: 'stripe', payment_intent: this.paymentIntent });
  }
  render() {
    // get account id and publishable key from the stripe processor for this mode.
    const data = getProcessorData(this.processors, 'stripe', this.mode);
    return (h("sc-stripe-payment-element", { order: this.order, address: this.address, "client-secret": this.clientSecret, publishableKey: data === null || data === void 0 ? void 0 : data.publishable_key, accountId: data === null || data === void 0 ? void 0 : data.account_id, key: this.clientSecret }));
  }
  static get is() { return "sc-order-stripe-payment-element"; }
  static get originalStyleUrls() { return {
    "$": ["sc-order-stripe-payment-element.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-order-stripe-payment-element.css"]
  }; }
  static get properties() { return {
    "processors": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Processor[]",
        "resolved": "Processor[]",
        "references": {
          "Processor": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Available processors"
      },
      "defaultValue": "[]"
    },
    "mode": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'test' | 'live'",
        "resolved": "\"live\" | \"test\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Payment mode."
      },
      "attribute": "mode",
      "reflect": false,
      "defaultValue": "'live'"
    },
    "currencyCode": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The currency code for the payment element."
      },
      "attribute": "currency-code",
      "reflect": false,
      "defaultValue": "'usd'"
    },
    "order": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order",
        "resolved": "Order",
        "references": {
          "Order": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The order."
      }
    },
    "address": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Should we collect an address?"
      },
      "attribute": "address",
      "reflect": false
    }
  }; }
  static get states() { return {
    "stripe": {},
    "clientSecret": {},
    "paymentIntent": {},
    "error": {},
    "confirming": {}
  }; }
  static get events() { return [{
      "method": "scPaid",
      "name": "scPaid",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }, {
      "method": "scPayError",
      "name": "scPayError",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "scSetPaymentIntent",
      "name": "scSetPaymentIntent",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{ processor: 'stripe'; payment_intent: PaymentIntent }",
        "resolved": "{ processor: \"stripe\"; payment_intent: PaymentIntent; }",
        "references": {
          "PaymentIntent": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      }
    }]; }
  static get watchers() { return [{
      "propName": "order",
      "methodName": "handleOrderChange"
    }, {
      "propName": "paymentIntent",
      "methodName": "handlePaymentIntentChange"
    }]; }
}
