function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useState } from 'react';
import JsonEditor from '../jsonEditor';
import { classPrefix } from '.';
import ZModulesCDP from '../../types';
import jsonUtil from '../jsonEditor/util';
import _ from 'lodash';
import { Button, Empty } from 'antd';
var ApiResponse = function ApiResponse(_ref) {
  var structure = _ref.structure,
    json = _ref.json,
    value = _ref.value,
    testing = _ref.testing,
    onChange = _ref.onChange,
    onTest = _ref.onTest;
  var _useState = useState(value || []),
    _useState2 = _slicedToArray(_useState, 2),
    checkedList = _useState2[0],
    setCheckedList = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    testLoading = _useState4[0],
    setTestLoading = _useState4[1];
  useEffect(function () {
    if (onChange) onChange(checkedList);
  }, [checkedList]);
  useEffect(function () {
    // 根据默认结构，初始化选中列表
    if (!structure || !structure.length || _.isEqual(structure, checkedList)) return;
    setCheckedList(structure);
  }, [structure]);
  function _onClick(label, path) {
    if (!label || !path) return;
    // 如果点击的是对象数组，则默认选中对象数组本身
    var prop = jsonUtil.getPropByPath(json, path);
    if (_.isArray(prop) && prop.length > 0 && _.isObject(prop[0])) {
      var element = prop[0];
      for (var key in element) {
        var newPath = "".concat(path, "[*].").concat(key);
        update(newPath);
      }
    } else {
      update(path);
    }
  }
  function update(targetPath) {
    setCheckedList(function (preList) {
      var newList = preList.filter(function (item) {
        // 如果path符合已选的*规则，则将对应的*规则删除
        var reg = new RegExp(_.escapeRegExp(item.rule).replace('\\[\\*\\]', '\\[\\d+\\]') + '$');
        if (reg.test(targetPath)) {
          return false;
        }
        return item.rule !== targetPath;
      });
      if (newList.length === preList.length) {
        var prop = jsonUtil.getPropByPath(json, targetPath);
        var type;
        switch (_typeof(prop)) {
          case 'number':
            type = ZModulesCDP.WebType.number;
            break;
          default:
            type = ZModulesCDP.WebType.string;
            break;
        }
        newList.push({
          rule: targetPath,
          type: type,
          required: true
        });
      }
      return newList;
    });
  }
  function labelRender(label, path) {
    var checked = checkedList.some(function (item) {
      // 如果规则中带有[*]，则进行规则匹配
      var reg = new RegExp(_.escapeRegExp(item.rule).replace('\\[\\*\\]', '\\[\\d+\\]') + '$');
      return reg.test(path);
    });
    return /*#__PURE__*/React.createElement("span", {
      className: "".concat(classPrefix, "-json-label ").concat(checked ? 'checked' : ''),
      onClick: function onClick(e) {
        e.stopPropagation();
        _onClick(label, path);
      }
    }, label);
  }
  var json_;
  try {
    json_ = JSON.stringify(json);
  } catch (e) {
    json_ = '';
  }
  if (!json_ || testLoading || testing) return /*#__PURE__*/React.createElement(Empty, {
    image: Empty.PRESENTED_IMAGE_SIMPLE
  }, /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    onClick: function onClick() {
      setTestLoading(true);
      onTest === null || onTest === void 0 ? void 0 : onTest(function (flag) {
        setTestLoading(flag);
      });
    },
    loading: testLoading || testing
  }, "\u6D4B\u8BD5\u8FDE\u63A5"));
  return /*#__PURE__*/React.createElement(JsonEditor, {
    value: json_,
    labelRender: labelRender
  });
};
export default ApiResponse;