function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import update from 'immutability-helper';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';
import Button from '../../../common/Button';
import DragAndDropHOC from '../../../decorator/DragAndDropHOC';
import DragCard from './DragCard';
import './index.less';
var operTypeMap = {
  MOVE_UP: '1',
  MOVE_DOWN: '2',
  COPY: '3',
  DELETE: '4'
};
export default function PageCompList(_ref) {
  var setPageData = _ref.setPageData,
      pageData = _ref.pageData,
      selected = _ref.selected,
      onAdd = _ref.onAdd,
      renderPagesManage = _ref.renderPagesManage;

  var _useState = useState(pageData || []),
      _useState2 = _slicedToArray(_useState, 2),
      components = _useState2[0],
      setComponents = _useState2[1];

  var ref = useRef();
  useEffect(function () {
    setComponents(pageData || []);
  }, [pageData]);
  var moveCard = useCallback(function (dragIndex, hoverIndex) {
    setComponents(function (prevCards) {
      var newComps = update(prevCards, {
        $splice: [[dragIndex, 1], [hoverIndex, 0, prevCards[dragIndex]]]
      });
      ref.current = newComps;
      return newComps;
    });
  }, []);

  var cardOnDrop = function cardOnDrop() {
    setPageData(ref.current);
  };

  var onMenuClick = function onMenuClick(index, operType) {
    if (operType === operTypeMap.MOVE_UP) {
      setComponents(function (data) {
        var newComponents = _toConsumableArray(data);

        var temp = newComponents[index - 1];
        newComponents[index - 1] = newComponents[index];
        newComponents[index] = temp;
        ref.current = newComponents;
        return newComponents;
      });
    }

    if (operType === operTypeMap.MOVE_DOWN) {
      setComponents(function (data) {
        var newComponents = _toConsumableArray(data);

        var temp = newComponents[index + 1];
        newComponents[index + 1] = newComponents[index];
        newComponents[index] = temp;
        ref.current = newComponents;
        return newComponents;
      });
    }

    if (operType === operTypeMap.COPY) {
      setComponents(function (data) {
        var newComponents = _toConsumableArray(data);

        newComponents.splice(index + 1, 0, _objectSpread({}, newComponents[index], {
          id: uuidv4()
        }));
        ref.current = newComponents;
        return newComponents;
      });
    }

    if (operType === operTypeMap.DELETE) {
      setComponents(function (data) {
        var newComponents = _toConsumableArray(data);

        newComponents.splice(index, 1);
        ref.current = newComponents;
        return newComponents;
      });
    }

    setPageData(ref.current);
  };

  var renderCard = useCallback(function (comp, index) {
    return /*#__PURE__*/React.createElement(DragCard, {
      data: comp,
      key: comp.id,
      index: index,
      moveCard: moveCard,
      ondrop: cardOnDrop,
      active: selected.id === comp.id,
      length: components.length,
      onMenuClick: onMenuClick
    });
  }, [selected]);
  return /*#__PURE__*/React.createElement("div", {
    className: "page-comp-wrap"
  }, /*#__PURE__*/React.createElement("div", {
    className: "page-comp-head"
  }, renderPagesManage), /*#__PURE__*/React.createElement("div", {
    className: "page-comp-list"
  }, /*#__PURE__*/React.createElement("div", {
    className: "title mb16"
  }, "Men's home page"), /*#__PURE__*/React.createElement(Button, {
    type: "light",
    size: "large",
    icon: "icon-add",
    onClick: onAdd
  }, "Add Component"), /*#__PURE__*/React.createElement("div", {
    className: "list mt16"
  }, /*#__PURE__*/React.createElement("div", {
    className: "inner-list"
  }, /*#__PURE__*/React.createElement(DragAndDropHOC, null, /*#__PURE__*/React.createElement("div", null, components.length > 0 && components.map(function (comp, i) {
    return renderCard(comp, i);
  })))))));
}