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 { Radio, Skeleton, Space, Tabs } from 'antd';
import React, { useEffect, useState } from 'react';
import ParamTable from './paramTable';
import JsonEditor from '../jsonEditor';
import { classPrefix } from '.';
import JSONRowBtns from './jsonRow';
var RequestParam = function RequestParam(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    headers = _useState2[0],
    setHeaders = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    parameters = _useState4[0],
    setParameters = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    body = _useState6[0],
    setBody = _useState6[1];
  var _useState7 = useState(''),
    _useState8 = _slicedToArray(_useState7, 2),
    json = _useState8[0],
    setJson = _useState8[1];
  var _useState9 = useState('form-data'),
    _useState10 = _slicedToArray(_useState9, 2),
    paramType = _useState10[0],
    setParamType = _useState10[1];
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    pageSizeColumn = _useState12[0],
    setPageSizeColumn = _useState12[1];
  var _useState13 = useState(),
    _useState14 = _slicedToArray(_useState13, 2),
    pageNumberColumn = _useState14[0],
    setPageNumberColumn = _useState14[1];
  var _useState15 = useState(true),
    _useState16 = _slicedToArray(_useState15, 2),
    initing = _useState16[0],
    setIniting = _useState16[1];
  useEffect(function () {
    if (!value) {
      setIniting(false);
      return;
    }
    var params = value.params,
      headers = value.headers,
      paramType = value.paramType,
      body = value.body,
      pageSizeColumn = value.pageSizeColumn,
      pageNumberColumn = value.pageNumberColumn;
    var paramsList = [];
    var headersList = [];
    var bodyList = [];
    Object.keys(params).forEach(function (key) {
      paramsList.push({
        name: key,
        value: params[key],
        page: pageSizeColumn === key ? 'pageSize' : pageNumberColumn === key ? 'pageNumber' : ''
      });
    });
    Object.keys(headers).forEach(function (key) {
      headersList.push({
        id: Math.random(),
        name: key,
        value: headers[key],
        page: pageSizeColumn === key ? 'pageSize' : pageNumberColumn === key ? 'pageNumber' : ''
      });
    });
    Object.keys(body).forEach(function (key) {
      bodyList.push({
        id: Math.random(),
        name: key,
        value: body[key],
        page: pageSizeColumn === key ? 'pageSize' : pageNumberColumn === key ? 'pageNumber' : ''
      });
    });
    setHeaders(headersList);
    setParameters(paramsList);
    setBody(bodyList);
    setParamType(paramType);
    if (paramType === 'json') {
      setJson(JSON.stringify(body, null, 2));
    }
    setPageSizeColumn(pageSizeColumn);
    setPageNumberColumn(pageNumberColumn);
    setIniting(false);
  }, []);
  function getValue() {
    var result = {
      params: {},
      headers: {},
      paramType: paramType,
      body: {},
      pageSizeColumn: pageSizeColumn,
      pageNumberColumn: pageNumberColumn
    };
    parameters.forEach(function (item) {
      result.params[item.name] = item.value;
      if (item.page === 'pageSize') {
        result.pageSizeColumn = item.name;
      }
      if (item.page === 'pageNumber') {
        result.pageNumberColumn = item.name;
      }
    });
    headers.forEach(function (item) {
      result.headers[item.name] = item.value;
    });
    if (paramType === 'json' && json) {
      result.body = JSON.parse(json);
    } else {
      body.forEach(function (item) {
        result.body[item.name] = item.value;
        if (item.page === 'pageSize') {
          result.pageSizeColumn = item.name;
        }
        if (item.page === 'pageNumber') {
          result.pageNumberColumn = item.name;
        }
      });
    }
    return result;
  }
  useEffect(function () {
    var result = getValue();
    onChange === null || onChange === void 0 ? void 0 : onChange(result);
  }, [headers, parameters, body, paramType, json, pageSizeColumn, pageNumberColumn]);
  var items = [{
    key: 'parameters',
    label: 'Parameters',
    children: /*#__PURE__*/React.createElement(ParamTable, {
      showPage: true,
      defaultValue: parameters,
      onChange: setParameters
    })
  }, {
    key: 'headers',
    label: 'Headers',
    children: /*#__PURE__*/React.createElement(ParamTable, {
      onChange: setHeaders,
      defaultValue: headers
    })
  }, {
    key: 'body',
    label: 'Body',
    children: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Space, {
      direction: "vertical",
      size: 16,
      style: {
        width: '100%'
      }
    }, /*#__PURE__*/React.createElement(Radio.Group, {
      onChange: function onChange(e) {
        setParamType(e.target.value);
      },
      value: paramType
    }, /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(Radio, {
      value: "form-data"
    }, "form-data"), /*#__PURE__*/React.createElement(Radio, {
      value: "x-www-form-urlencoded"
    }, "x-www-form-urlencoded"), /*#__PURE__*/React.createElement(Radio, {
      value: "json"
    }, "json"))), paramType === 'json' ? /*#__PURE__*/React.createElement(JsonEditor, {
      defaultMode: "edit",
      value: json,
      readOnly: false,
      onChange: setJson,
      rowClassName: "".concat(classPrefix, "-json-row"),
      columnExtra: function columnExtra(label, val, path) {
        if (!path || !label) return null;
        var value;
        if (path === pageSizeColumn) {
          value = 'pathSize';
        } else if (path === pageNumberColumn) {
          value = 'pageNumber';
        }
        return /*#__PURE__*/React.createElement(JSONRowBtns, {
          value: value,
          onChange: function onChange(type) {
            switch (type) {
              case 'pathSize':
                setPageSizeColumn(path);
                if (pageNumberColumn === path) setPageNumberColumn(undefined);
                break;
              case 'pageNumber':
                setPageNumberColumn(path);
                if (pageSizeColumn === path) setPageSizeColumn(undefined);
                break;
              default:
                break;
            }
          }
        });
      }
    }) : /*#__PURE__*/React.createElement(ParamTable, {
      showPage: true,
      defaultValue: body,
      onChange: setBody
    })))
  }];
  if (initing) return /*#__PURE__*/React.createElement(Skeleton, {
    active: true,
    paragraph: {
      rows: 10
    }
  });
  return /*#__PURE__*/React.createElement(Tabs, {
    items: items,
    size: "large"
  });
};
export default RequestParam;