"use strict";

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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = PageCompList;

var _antd = require("antd");

var _classnames = _interopRequireDefault(require("classnames"));

var _immutabilityHelper = _interopRequireDefault(require("immutability-helper"));

var _react = _interopRequireWildcard(require("react"));

var _common = require("../../../common");

var _coreUtil = require("../../../utils/coreUtil");

require("./index.less");

var _constants = require("../../../constants");

var _mobx = require("../../../mobx");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var __spreadArrays = void 0 && (void 0).__spreadArrays || function () {
  for (var s = 0, i = 0, il = arguments.length; i < il; i++) {
    s += arguments[i].length;
  }

  for (var r = Array(s), k = 0, i = 0; i < il; i++) {
    for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) {
      r[k] = a[j];
    }
  }

  return r;
};

var operTypeMap = {
  MOVE_UP: '1',
  MOVE_DOWN: '2',
  COPY: '3',
  DELETE: '4'
};

function PageCompList(_a) {
  var setPageData = _a.setPageData,
      pageData = _a.pageData,
      onAdd = _a.onAdd,
      renderPagesManage = _a.renderPagesManage,
      pageName = _a.pageName;

  var _b = (0, _react.useState)(pageData || []),
      components = _b[0],
      setComponents = _b[1];

  var _c = (0, _react.useState)(''),
      dragId = _c[0],
      setDragId = _c[1];

  var _d = (0, _react.useState)(''),
      clickedFloor = _d[0],
      setClickedFloor = _d[1];

  var ref = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    setComponents(pageData || []);
  }, [pageData]);
  (0, _react.useEffect)(function () {
    var distroy = (0, _mobx.autorun)(function () {
      setClickedFloor(_mobx.store.clickedFloor);
    });
    return distroy;
  }, []);
  var moveCard = (0, _react.useCallback)(function (dragNodeId, hoverNodeId) {
    var dragIndex = components.findIndex(function (comp) {
      return comp.id === dragNodeId;
    });
    var hoverIndex = components.findIndex(function (comp) {
      return comp.id === hoverNodeId;
    });
    setComponents(function (prevCards) {
      var newComps = (0, _immutabilityHelper["default"])(prevCards, {
        $splice: [[dragIndex, 1], [hoverIndex, 0, prevCards[dragIndex]]]
      });
      ref.current = newComps;
      return newComps;
    });
  }, [components]);

  var onDragStart = function onDragStart(e) {
    setDragId(e.currentTarget.dataset.nodeid);
  };

  var onDragEnter = function onDragEnter(e) {
    var hoverNodeId = e.currentTarget.dataset.nodeid;

    if (dragId === hoverNodeId) {
      return;
    }

    moveCard(dragId, hoverNodeId);
  };

  var onDragEnd = function onDragEnd() {
    setDragId('');
    setPageData(ref.current);
  };

  var onMenuClick = function onMenuClick(index, operType) {
    if (operType === operTypeMap.MOVE_UP) {
      setComponents(function (data) {
        var newComponents = __spreadArrays(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 = __spreadArrays(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 = __spreadArrays(data);

        var copyComponent = (0, _coreUtil.copyNode)(newComponents[index]);
        newComponents.splice(index + 1, 0, copyComponent);
        ref.current = newComponents;
        return newComponents;
      });
    }

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

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

    setPageData(ref.current);
  };

  var componentScrollTop = function componentScrollTop(data) {
    if (document.getElementById("view" + data.id)) {
      document.getElementById("view" + data.id).scrollIntoView({
        behavior: 'smooth'
      });
    }
  };

  var onCardClick = function onCardClick(event, data) {
    componentScrollTop(data); // 这里必须阻止事件冒泡，然后再触发自定义的事件

    event.preventDefault();
    event.stopPropagation();
    var evt = new Event('component-click', {
      bubbles: false
    });
    evt.data = data;
    window.document.dispatchEvent(evt);
    if (clickedFloor === data.id) return;

    _mobx.store.setState({
      clickedFloor: data.id
    });
  };

  var renderCard = function renderCard(comp, index) {
    var menu = /*#__PURE__*/_react["default"].createElement(_antd.Menu, {
      onClick: function onClick(_a) {
        var item = _a.item;
        return onMenuClick(index, item.props.value);
      }
    }, index !== 0 && /*#__PURE__*/_react["default"].createElement(_antd.Menu.Item, {
      className: "menu_item",
      value: "1"
    }, "Move Up"), index !== components.length - 1 && /*#__PURE__*/_react["default"].createElement(_antd.Menu.Item, {
      className: "menu_item",
      value: "2"
    }, "Move Down"), /*#__PURE__*/_react["default"].createElement(_antd.Menu.Item, {
      className: "menu_item",
      value: "3"
    }, "Copy"), /*#__PURE__*/_react["default"].createElement(_antd.Menu.Item, {
      className: "menu_item",
      value: "4"
    }, "Delete"));

    var dragging = dragId === String(comp.id);
    var active = clickedFloor === String(comp.id);
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: "card" + comp.id,
      className: (0, _classnames["default"])('card-wrap', {
        active: active
      }),
      "data-nodeid": comp.id,
      draggable: "true",
      id: "card" + comp.id,
      onDragEnd: onDragEnd,
      onDragEnter: onDragEnter,
      onDragOver: function onDragOver(e) {
        e.preventDefault();
      },
      onDragStart: onDragStart,
      style: {
        opacity: dragging ? '0.4' : '1'
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "left",
      onClick: function onClick(e) {
        return onCardClick(e, comp);
      }
    }, /*#__PURE__*/_react["default"].createElement(_common.Iconfont, {
      className: "comp-icon",
      color: active ? '#2F54EB' : '',
      size: "20px",
      type: _constants.compInfoMap[comp.type].icon
    }), /*#__PURE__*/_react["default"].createElement(_common.Iconfont, {
      className: "drag-icon",
      color: "#2F54EB",
      size: "20px",
      type: "icon-drag"
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: "card-title ml16"
    }, index + 1, ". ", _constants.compInfoMap[comp.type].name)), /*#__PURE__*/_react["default"].createElement(_antd.Dropdown, {
      overlay: menu,
      placement: "bottomRight",
      trigger: ['click']
    }, /*#__PURE__*/_react["default"].createElement(_common.Iconfont, {
      className: "more-btn",
      color: "#2F54EB",
      size: "20px",
      style: {
        height: '40px'
      },
      type: "icon-more"
    })));
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-comp-wrap"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-comp-head"
  }, renderPagesManage), /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-comp-list"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "title mb16"
  }, pageName), /*#__PURE__*/_react["default"].createElement(_common.Button, {
    icon: "icon-add",
    onClick: onAdd,
    size: "large",
    type: "light"
  }, "Add Component"), /*#__PURE__*/_react["default"].createElement("div", {
    className: "list mt16"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "inner-list"
  }, /*#__PURE__*/_react["default"].createElement("div", null, components.length > 0 && components.map(function (comp, i) {
    return renderCard(comp, i);
  }))))));
}