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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
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;