function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useState, useRef } from 'react';
import { Button, Input } from 'antd';
import { EButtonKeyType } from "./types";

/**
 * @title 渲染动态内容
 * @description 用于渲染从接口解析出来的元素
 */

var Action = function Action(props) {
  var _data$layouts2, _data$layouts3;
  var data = props.data,
    _props$cancelDisabled = props.cancelDisabled,
    cancelDisabled = _props$cancelDisabled === void 0 ? false : _props$cancelDisabled,
    _props$isLoading = props.isLoading,
    isLoading = _props$isLoading === void 0 ? false : _props$isLoading;
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    inputValues = _useState2[0],
    setInputValues = _useState2[1];
  var inputValuesRef = useRef(inputValues);
  useEffect(function () {
    var _data$layouts;
    if (!data) return;
    var inputObj = {};
    data === null || data === void 0 || (_data$layouts = data.layouts) === null || _data$layouts === void 0 || _data$layouts.forEach(function (item) {
      var _item$elements;
      item === null || item === void 0 || (_item$elements = item.elements) === null || _item$elements === void 0 || _item$elements.forEach(function (element) {
        if ((element === null || element === void 0 ? void 0 : element.type) === 'input' && element !== null && element !== void 0 && element.name) {
          inputObj[element === null || element === void 0 ? void 0 : element.name] = inputValuesRef.current[element === null || element === void 0 ? void 0 : element.name] || '';
        }
      });
    });
    setInputValues(inputObj);
  }, [data]);
  useEffect(function () {
    inputValuesRef.current = inputValues;
  }, [inputValues]);
  var handleInputChange = function handleInputChange(key, value) {
    setInputValues(function (prev) {
      return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, key, value));
    });
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, isLoading && (data === null || data === void 0 ? void 0 : data.message) && /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip"
  }, data === null || data === void 0 ? void 0 : data.message), (data === null || data === void 0 || (_data$layouts2 = data.layouts) === null || _data$layouts2 === void 0 ? void 0 : _data$layouts2.length) > 0 ? /*#__PURE__*/React.createElement("div", {
    style: {
      maxWidth: '100%',
      width: '100%'
    }
  }, data === null || data === void 0 || (_data$layouts3 = data.layouts) === null || _data$layouts3 === void 0 ? void 0 : _data$layouts3.map(function (item, index) {
    var _item$elements2;
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: "pisell-lowcode__eft-pay-step-item-btns",
      style: {
        marginTop: '20px',
        width: '100%',
        display: 'flex',
        justifyContent: 'center'
      }
    }, item === null || item === void 0 || (_item$elements2 = item.elements) === null || _item$elements2 === void 0 ? void 0 : _item$elements2.map(function (element) {
      var key = element.key,
        label = element.label,
        type = element.type,
        text = element.text,
        name = element.name;
      if (type === 'button') {
        return /*#__PURE__*/React.createElement(Button, {
          key: key + label,
          block: true,
          className: "pisell-lowcode__eft-pay-step-item-btn",
          onClick: function onClick() {
            var _props$onClick;
            return props === null || props === void 0 || (_props$onClick = props.onClick) === null || _props$onClick === void 0 ? void 0 : _props$onClick.call(props, element, {
              inputValues: inputValues
            });
          },
          disabled: key === EButtonKeyType.CancelTransaction ? cancelDisabled : false
        }, element.label);
      } else if (type === 'text') {
        return /*#__PURE__*/React.createElement("div", {
          key: key + label,
          className: "pisell-lowcode__eft-pay-step-item-tip"
        }, label ? "".concat(label, ": ") : '', text || '');
      } else if (type === 'image') {
        var _ref = (element === null || element === void 0 ? void 0 : element.image) || {},
          _name = _ref.name,
          _data = _ref.data,
          encoding = _ref.encoding;
        var imageUrl = _data ? "data:".concat(_name || 'image/png', ";").concat(encoding || 'base64', ",").concat(_data) : '';
        return /*#__PURE__*/React.createElement("div", {
          style: {
            width: '100%',
            display: 'flex',
            justifyContent: 'center'
          }
        }, /*#__PURE__*/React.createElement("img", {
          key: key + label,
          src: imageUrl,
          alt: label
        }));
      } else if (type === 'input') {
        return /*#__PURE__*/React.createElement("div", {
          style: {
            width: '100%',
            display: 'flex',
            alignItems: 'center',
            flexWrap: 'wrap'
          }
        }, /*#__PURE__*/React.createElement("span", {
          style: {
            marginRight: '10px'
          }
        }, "".concat(label ? "".concat(label, " ") : '')), /*#__PURE__*/React.createElement(Input, {
          key: key + label,
          alt: label,
          style: {
            flex: 1,
            minWidth: '200px'
          },
          value: inputValues[name] || '',
          onChange: function onChange(e) {
            return handleInputChange(name, e.target.value);
          }
        }));
      }
      return null;
    }));
  })) : null, (data === null || data === void 0 ? void 0 : data.detailsText) && /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip",
    style: {
      marginTop: '24px'
    }
  }, data === null || data === void 0 ? void 0 : data.detailsText));
};
export default Action;