function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/**
 * Copyright (c) 2022 International Business Group, Ant Group. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), the rights to use, copy, modify, merge, and/or distribute the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 1. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE; and
 * 2. If applicable, the use of the Software is also subject to the terms and conditions of any non-disclosure agreement signed by you and the relevant Ant Group entity.
 */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { BASEPLUGINID, ERRORMESSAGE, EVENT } from "../../constant";
import CheckoutApp from "../../plugin/drop-in";
import { MessageName, networkModeEnum } from "../../types";
import { createIframeNode } from "../../util/createIframeNode";
export var mixinDropInConstructor = function mixinDropInConstructor(options, context) {
  // Instantiate the plug-in and set render
  var checkoutPlugin = new CheckoutApp();
  checkoutPlugin.setRender(createIframeNode);
  // Register Cashier Application Plug in
  context._registerPlugin(BASEPLUGINID, checkoutPlugin);
  context._subscribeFromCheckoutPlugin();
};

/**
 * @description Create checkout application
 * @description drop-in
 */
export var createPayment = function createPayment(params) {
  var _this = this;
  if (this.options.networkMode === networkModeEnum.proxy) {
    return new Promise(function (resolve, reject) {
      try {
        var checkoutApp = _this._getPlugin(BASEPLUGINID);
        resolve(checkoutApp.render(params));
      } catch (error) {
        reject(error);
      }
    });
  } else {
    console.warn(ERRORMESSAGE.INIT_PARAMETER_ERROR.NET_MODE_NOT_SUPPORT.message);
    return Promise.reject(ERRORMESSAGE.INIT_PARAMETER_ERROR.NET_MODE_NOT_SUPPORT);
  }
};
/**
 * @description Take action on the interface result of payment submission
 * @description drop-in
 */
export var confirmPayment = function confirmPayment(result) {
  var _this2 = this;
  return new Promise(function (resolve, reject) {
    _this2.eventCenter.once('onRedirect', function (payload) {
      _this2._redirect(payload).then(function () {
        resolve();
      }).catch(function (err) {
        reject(err);
      });
    });
    _this2.eventCenter.emit("".concat(BASEPLUGINID, "-handleRequestPayment"), {
      name: MessageName.SDK_TO_APP,
      mode: _this2.options.mode,
      appId: BASEPLUGINID,
      instanceId: _this2._instanceId,
      context: {
        event: 'handleRequestPayment',
        data: result
      }
    });
  });
};
/**
 * @description Subscribe to the message of the cashier application plug-in
 */
export var _subscribeFromCheckoutPlugin = function _subscribeFromCheckoutPlugin() {
  var _this3 = this;
  var eventKeyMap = new Map([[EVENT.error.name, function (payload) {
    _this3.eventCenter.emit(EVENT.error.name, payload.context.data);
  }], [EVENT.ready.name, function (payload) {
    _this3.eventCenter.emit(EVENT.ready.name, payload.context.data);
  }], [EVENT.redirect.name, function (payload) {
    _this3.eventCenter.emit(EVENT.redirect.name, payload.context.data);
  }], [EVENT.paymentMethodSelected.name, function (payload) {
    _this3.checkoutState.paymentMethodType = payload.context.data.paymentMethodType;
    _this3.eventCenter.emit(EVENT.paymentMethodSelected.name, {
      paymentMethodType: payload.context.data.paymentMethodType
    });
  }], [EVENT.sizeChanged.name, function (payload) {
    _this3.eventCenter.emit(EVENT.sizeChanged.name, payload.context.data);
  }], [EVENT.log.name, function (payload) {
    _this3.eventCenter.emit(EVENT.log.name, payload.context.data);
  }]]);
  var _iterator = _createForOfIteratorHelper(eventKeyMap.keys()),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var key = _step.value;
      this.eventCenter.on("".concat(BASEPLUGINID, "-").concat(key), eventKeyMap.get(key));
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
};