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 { 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'),
    _useState0 = _slicedToArray(_useState9, 2),
    paramType = _useState0[0],
    setParamType = _useState0[1];
  var _useState1 = useState(),
    _useState10 = _slicedToArray(_useState1, 2),
    pageSizeColumn = _useState10[0],
    setPageSizeColumn = _useState10[1];
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    pageNumberColumn = _useState12[0],
    setPageNumberColumn = _useState12[1];
  var _useState13 = useState(true),
    _useState14 = _slicedToArray(_useState13, 2),
    initing = _useState14[0],
    setIniting = _useState14[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;