/**
 * 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.
 */
import { COMPONENT_CONTAINER_ID, COMPONENT_IFRAME_TAG_ID, INLINE_BASE_STYLE_ID, INLINE_IMG_CLASSNAME, LOADING_ID } from "../../constant";
import { ComponentSignEnum, PlatformEnum } from "../../types";
import { amsSetSize } from "../../util";
// border-radius: 8px;
var inlineComponentCss = "#".concat(COMPONENT_IFRAME_TAG_ID, "-desktop{\n  position: absolute;\n  top: 0;\n  left: 0;\n}\n#").concat(COMPONENT_IFRAME_TAG_ID, "-mobile{\n  position: absolute;\n  top: 0;\n  left: 0;\n}\n.").concat(COMPONENT_CONTAINER_ID, "-inline{\n  width: 100%;\n  height: auto;\n  position: relative;\n  line-height: 0;\n  display: flex;\n}\n#").concat(LOADING_ID, "{\n  width: 100%;\n  flex: 1;\n}\n.").concat(INLINE_IMG_CLASSNAME, "{\n  width: 100%;\n  height: auto;\n  object-fit: contain;\n  box-sizing: border-box;\n}\n.").concat(INLINE_IMG_CLASSNAME, "-mobile{\n  padding: 0 0.16em;\n}\n.").concat(INLINE_IMG_CLASSNAME, "-desktop{}\n");
export var inlineComponentAddCss = function inlineComponentAddCss() {
  var style = document.createElement('style');
  style.id = 'ams-inline-component-style';
  style.type = 'text/css';
  style.innerHTML = inlineComponentCss;
  document.head.appendChild(style);
};
/**
 * @description ComponentSignEnum.ELEMENT_PAYMENT特殊处理
 * @param {Options['type']} type
 */
var renderElementPayment = function renderElementPayment(type) {
  var spliceStr = function spliceStr() {
    var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    return arr.reduce(function (pre, cur) {
      return pre += cur;
    }, '');
  };
  var commonDom = function commonDom(children) {
    var viewBox = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0 0 560 256';
    var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 256;
    return "<svg id=\"".concat(INLINE_IMG_CLASSNAME, "-").concat(type, "\" width=\"100%\" height=\"").concat(height, "\" viewBox=\"").concat(viewBox, "\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\" preserveAspectRatio=\"none\">\n    ").concat(spliceStr(children), "\n    <animate xlink:href=\"#").concat(INLINE_IMG_CLASSNAME, "-").concat(type, "\" attributeName=\"opacity\" values=\"0.5; 1; 0.5\" begin=\"0s\" dur=\"1s\" repeatCount=\"indefinite\"></animate>\n  </svg>");
  };
  var creatRect = function creatRect(_ref) {
    var _ref$x = _ref.x,
      x = _ref$x === void 0 ? 0 : _ref$x,
      _ref$y = _ref.y,
      y = _ref$y === void 0 ? 0 : _ref$y,
      _ref$fill = _ref.fill,
      fill = _ref$fill === void 0 ? 'black' : _ref$fill,
      _ref$width = _ref.width,
      width = _ref$width === void 0 ? 560 : _ref$width,
      _ref$height = _ref.height,
      height = _ref$height === void 0 ? 32 : _ref$height,
      _ref$rx = _ref.rx,
      rx = _ref$rx === void 0 ? 4 : _ref$rx;
    return "<rect fill-opacity=\"0.04\" x=\"".concat(x, "\" y=\"").concat(y, "\" width=\"").concat(width, "\" height=\"").concat(height, "\" rx=\"").concat(rx, "\" fill=\"").concat(fill, "\"/>");
  };
  var result = [creatRect({
    fill: 'white'
  }), creatRect({
    y: 4
  }), creatRect({
    y: 48,
    height: 64
  }), creatRect({
    y: 128
  }), creatRect({
    y: 176
  }), creatRect({
    y: 224
  })];
  if (type === 'tabs') {
    result = [creatRect({
      fill: 'white',
      width: 358,
      height: 305
    }), creatRect({
      y: 4,
      width: '50%',
      height: 73
    }), creatRect({
      x: '51%',
      y: 4,
      height: 73
    }), creatRect({
      y: 96,
      height: 64
    }), creatRect({
      y: 178
    }), creatRect({
      y: 228
    }), creatRect({
      y: 276
    })];
  }
  var viewBox = {
    view: type === 'tabs' ? '0 0 358 305' : '0 0 560 256',
    height: type === 'tabs' ? 305 : 256
  };
  return commonDom(result, viewBox.view, viewBox.height);
};
export var addInlineLoading = function addInlineLoading(_selector, platform, options) {
  var loading = document.createElement('div');
  loading.id = LOADING_ID;
  if (options.componentSign === ComponentSignEnum.ELEMENT_PAYMENT) {
    if (options.isExpressCheckout) {
      loading.innerHTML = '';
    } else {
      loading.innerHTML = renderElementPayment(options === null || options === void 0 ? void 0 : options.type);
    }
  } else if (platform === PlatformEnum.desktop) loading.innerHTML = "<svg class=\"".concat(INLINE_IMG_CLASSNAME, " ").concat(INLINE_IMG_CLASSNAME, "-").concat(platform, "\" width=\"740\" height=\"360\" viewBox=\"0 0 740 360\" fill=\"none\" xmlns=\"\">\n  <rect width=\"740\" height=\"360\" rx=\"12\" fill=\"#F6F6F6\"/>\n  <rect opacity=\"0.7\" x=\"44\" y=\"44\" width=\"32\" height=\"32\" rx=\"4\" fill=\"#E1E6ED\"/>\n  <rect opacity=\"0.7\" x=\"94\" y=\"44\" width=\"32\" height=\"32\" rx=\"4\" fill=\"#E1E6ED\"/>\n  <rect opacity=\"0.7\" x=\"144\" y=\"44\" width=\"32\" height=\"32\" rx=\"4\" fill=\"#E1E6ED\"/>\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M136 100H48C43.5817 100 40 103.582 40 108C40 112.418 43.5817 116 48 116H136C140.418 116 144 112.418 144 108C144 103.582 140.418 100 136 100Z\" fill=\"#E1E6ED\"/>\n  </g>\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M682 126H58C48.0589 126 40 134.059 40 144C40 153.941 48.0589 162 58 162H682C691.941 162 700 153.941 700 144C700 134.059 691.941 126 682 126Z\" fill=\"#E1E6ED\"/>\n  </g>\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M682 182H58C48.0589 182 40 190.059 40 200C40 209.941 48.0589 218 58 218H682C691.941 218 700 209.941 700 200C700 190.059 691.941 182 682 182Z\" fill=\"#E1E6ED\"/>\n  </g>\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M504 274H64C52.9543 274 44 282.954 44 294C44 305.046 52.9543 314 64 314H504C515.046 314 524 305.046 524 294C524 282.954 515.046 274 504 274Z\" fill=\"#E1E6ED\"/>\n  </g>\n  </svg>");else if (platform === PlatformEnum.mobile) loading.innerHTML = "<svg class=\"".concat(INLINE_IMG_CLASSNAME, " ").concat(INLINE_IMG_CLASSNAME, "-").concat(platform, "\" width=\"358\" height=\"186\" viewBox=\"0 0 358 186\" fill=\"none\" xmlns=\"\">\n  <g clip-path=\"url(#clip0_2543_91192)\">\n  <rect width=\"358\" height=\"186\" fill=\"white\"/>\n  <g clip-path=\"url(#clip1_2543_91192)\">\n  <rect width=\"358\" height=\"186\" fill=\"white\"/>\n  <rect width=\"358\" height=\"40\" fill=\"white\"/>\n  <rect opacity=\"0.7\" x=\"4\" y=\"4\" width=\"32\" height=\"32\" rx=\"4\" fill=\"#E1E6ED\"/>\n  <rect opacity=\"0.7\" x=\"54\" y=\"4\" width=\"32\" height=\"32\" rx=\"4\" fill=\"#E1E6ED\"/>\n  <rect opacity=\"0.7\" x=\"104\" y=\"4\" width=\"32\" height=\"32\" rx=\"4\" fill=\"#E1E6ED\"/>\n  <g clip-path=\"url(#clip2_2543_91192)\">\n  <rect width=\"358\" height=\"120\" transform=\"translate(0 66)\" fill=\"white\"/>\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M95 66H9C4.02944 66 0 70.0294 0 75C0 79.9706 4.02943 84 9 84H95C99.9706 84 104 79.9706 104 75C104 70.0294 99.9706 66 95 66Z\" fill=\"#E1E6ED\"/>\n  </g>\n  <g clip-path=\"url(#clip3_2543_91192)\">\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M340 94H18C8.05888 94 0 102.059 0 112C0 121.941 8.05887 130 18 130H340C349.941 130 358 121.941 358 112C358 102.059 349.941 94 340 94Z\" fill=\"#E1E6ED\"/>\n  </g>\n  <g style=\"mix-blend-mode:multiply\" opacity=\"0.336007\">\n  <path d=\"M340 150H18C8.05888 150 0 158.059 0 168C0 177.941 8.05887 186 18 186H340C349.941 186 358 177.941 358 168C358 158.059 349.941 150 340 150Z\" fill=\"#E1E6ED\"/>\n  </g>\n  </g>\n  </g>\n  </g>\n  </g>\n  <defs>\n  <clipPath id=\"clip0_2543_91192\">\n  <rect width=\"358\" height=\"186\" fill=\"white\"/>\n  </clipPath>\n  <clipPath id=\"clip1_2543_91192\">\n  <rect width=\"358\" height=\"186\" fill=\"white\"/>\n  </clipPath>\n  <clipPath id=\"clip2_2543_91192\">\n  <rect width=\"358\" height=\"120\" fill=\"white\" transform=\"translate(0 66)\"/>\n  </clipPath>\n  <clipPath id=\"clip3_2543_91192\">\n  <rect width=\"358\" height=\"92\" fill=\"white\" transform=\"translate(0 94)\"/>\n  </clipPath>\n  </defs>\n  </svg>\n  ");
  if (_selector) _selector.appendChild(loading);
  var loadingDom = document.getElementById(LOADING_ID);
  amsSetSize(null, loadingDom);
};
export var createInlineBaseElement = function createInlineBaseElement(selector) {
  var containerId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : COMPONENT_CONTAINER_ID;
  var selectorId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  var inlineBaseStyle = document.getElementById(INLINE_BASE_STYLE_ID);
  if (!inlineBaseStyle) inlineComponentAddCss();
  if (document.getElementById(selectorId ? selectorId : containerId)) return;
  var amsComponentContainer = document.createElement('div');
  amsComponentContainer.className = "".concat(containerId, "-inline");
  amsComponentContainer.id = selectorId ? selectorId : containerId;
  var selectorDom = document.querySelector("".concat(selector));
  if (selectorDom) selectorDom.appendChild(amsComponentContainer);
  return amsComponentContainer;
};