"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"] = void 0;

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

var _uuid = require("uuid");

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

var _CompListPanel = _interopRequireDefault(require("./CompListPanel"));

require("./index.less");

var _PageCompList = _interopRequireDefault(require("./PageCompList"));

var _ConfigPanel = _interopRequireDefault(require("./ConfigPanel"));

var _QuickMenuBar = _interopRequireDefault(require("./QuickMenuBar"));

var _ViewArea = _interopRequireDefault(require("./ViewArea"));

var _componentTypes = require("../../constants/component-types");

var _mobxReact = require("mobx-react");

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

var _History = _interopRequireDefault(require("./History"));

var _antd = require("antd");

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

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

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (b.hasOwnProperty(p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

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 setDesignConfig = function setDesignConfig(data) {
  window.magicDesign = __assign(__assign({
    mode: 'designer',
    device: _constants.deviceTypeMap.PHONE
  }, data), {
    MBaseUrl: data.MBaseUrl || ""
  });
};

var debug = require('debug')('worker:Designer');
/**
 * @class Designer
 * 主入口。
 */


var Designer =
/** @class */
function (_super) {
  __extends(Designer, _super);

  function Designer(props) {
    var _this = _super.call(this, props) || this;

    _this.closeCompList = function () {
      if (_this.compListRef.current.open) {
        _this.compListRef.current.onClose();
      }
    };

    _this.openCompList = function () {
      return _this.compListRef.current.onOpen();
    };

    _this.openConfigPanel = function () {
      if (!_this.configRef.current) {
        return;
      }

      var device = _this.state.device;

      if (device === _constants.deviceTypeMap.PC) {
        _this.configRef.current.openPcConfig();

        return;
      }

      _this.configRef.current.openMobileConfig();
    };

    _this.closeConfigPanel = function () {
      if (!_this.configRef.current) {
        return;
      }

      var device = _this.state.device;

      if (!_mobx.store.clickedFloor) {
        return;
      }

      _this.configRef.current.closeSecConfig();

      if (device === _constants.deviceTypeMap.PC) {
        _this.configRef.current.closePcConfig();

        return;
      }

      _this.configRef.current.closeMobileConfig();
    };

    _this.reRenderViewArea = function () {
      if (!_this.viewAreaRef || !_this.viewAreaRef.current) {
        return;
      }

      _this.history.push(_this.state.MPageData);

      _this.viewAreaRef.current.renderView();
    };

    _this.componentClick = function (event) {
      _this.openConfigPanel();

      _this.onComponentClick(event.data);
    };
    /**
     * 楼层 的 新增、删除、拷贝、顺序调整、属性修改时调用
     * @param {*} pageData 模版信息
     */


    _this.setPageData = function (pageData) {
      var MPageData = _this.state.MPageData; // this.history.push(pageData);

      _this.setState({
        MPageData: _this.history.push(pageData)
      });
    };

    _this.onSwitchDevice = function (device) {
      if (device === window.magicDesign.device) {
        return;
      }

      _this.closeConfigPanel();

      _this.setState(__assign(__assign({}, _this.state), {
        device: device
      }));

      _this.closeCompList();

      _mobx.store.setState({
        clickedFloor: ''
      });

      window.magicDesign.device = device;
    };

    _this.onAdd = function () {
      _this.closeConfigPanel();

      _this.openCompList();

      _mobx.store.setState({
        clickedFloor: ''
      });
    };

    _this.onPropsPanelClose = function () {
      _mobx.store.setState({
        clickedFloor: ''
      });
    };

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

    _this.addComponent = function (_a) {
      var type = _a.type,
          label = _a.label,
          icon = _a.icon;
      var MPageData = _this.state.MPageData;

      var compInst = _componentTypes.componentMap[type].getDefaultJSON();

      var newData = __spreadArrays(MPageData, [compInst]);

      _this.history.push(newData);

      _mobx.store.setState({
        MPageData: newData
      });

      _this.setState({
        MPageData: newData
      }, function () {
        setTimeout(function () {
          // 定位到新增组件
          _this.componentScrollTop(compInst);

          var evt = new Event('component-click', {
            bubbles: false
          });
          evt.data = compInst;
          window.document.dispatchEvent(evt);
        }, 0);
      });
    };

    _this.onQuickMenuClick = function (code) {
      var _a = _this.props,
          onPublish = _a.onPublish,
          onSave = _a.onSave,
          onPreviews = _a.onPreviews;
      var _b = _this.state,
          device = _b.device,
          MPageData = _b.MPageData;
      var handleParams = {
        pageData: {
          id: (0, _uuid.v4)(),
          childNodes: MPageData
        },
        device: device
      };

      switch (code) {
        case _constants.quickMenuCode.UNDO:
          _this.setState({
            MPageData: _this.history.undo()
          });

          break;

        case _constants.quickMenuCode.REDO:
          _this.setState({
            MPageData: _this.history.redo()
          });

          break;

        case _constants.quickMenuCode.PUBLISH:
          onPublish(handleParams);
          break;

        case _constants.quickMenuCode.PREVIEW:
          onPreviews(handleParams);
          break;

        case _constants.quickMenuCode.SAVE:
          onSave(handleParams);
          break;

        case _constants.quickMenuCode.MODE_PC:
          _this.onSwitchDevice(_constants.deviceTypeMap.PC);

          break;

        case _constants.quickMenuCode.MODE_PHONE:
          _this.onSwitchDevice(_constants.deviceTypeMap.PHONE);

          break;
      }
    };

    _this.renderHeader = function () {
      return /*#__PURE__*/_react["default"].createElement(_QuickMenuBar["default"], {
        language: _this.props.language,
        undoDisable: !_this.history.undoAble(),
        redoDisable: !_this.history.redoAble(),
        onBtnClick: _this.onQuickMenuClick,
        device: _this.state.device
      });
    };

    var pageData = props.pageData,
        config = props.config;
    var MPageData = pageData.childNodes || [];

    _mobx.store.setState({
      MPageData: MPageData
    });

    _this.configRef = /*#__PURE__*/_react["default"].createRef();
    _this.viewAreaRef = /*#__PURE__*/_react["default"].createRef();
    _this.compListRef = /*#__PURE__*/_react["default"].createRef();
    _this.state = {
      MPageData: MPageData,
      device: _constants.deviceTypeMap.PHONE
    };
    _this.history = new _History["default"](MPageData);
    setDesignConfig(config);
    return _this;
  }

  Designer.prototype.componentWillReceiveProps = function (nextProps) {
    var pageData = nextProps.pageData,
        config = nextProps.config;
    var MPageData = pageData.childNodes || [];
    this.setState({
      MPageData: MPageData
    });

    _mobx.store.setState({
      MPageData: MPageData
    });

    this.history = new _History["default"](MPageData);
    setDesignConfig(config);
  };

  Designer.prototype.onComponentClick = function (data) {
    if (_mobx.store.clickedFloor === data.id) return;

    _mobx.store.setState({
      clickedFloor: data.id,
      clickedGroup: null,
      clickedMeta: null
    });
  };

  Designer.prototype.componentDidMount = function () {
    // 复合组件点击事件
    window.document.addEventListener('component-click', this.componentClick); // 复合组件重新渲染事件

    window.document.addEventListener('render-view', this.reRenderViewArea);
  };

  Designer.prototype.componentWillUnmount = function () {
    window.document.removeEventListener('component-click', this.componentClick);
    window.document.removeEventListener('render-view', this.reRenderViewArea);
  };

  Designer.prototype.render = function () {
    var _this = this;

    var _a = this.state,
        MPageData = _a.MPageData,
        device = _a.device;
    var _b = this.props,
        pagesManager = _b.pagesManager,
        compSource = _b.compSource,
        pageName = _b.pageName;
    return /*#__PURE__*/_react["default"].createElement(_mobxReact.Provider, {
      store: _mobx.store
    }, /*#__PURE__*/_react["default"].createElement(_antd.ConfigProvider, {
      getPopupContainer: function getPopupContainer(triggerNode) {
        return triggerNode ? triggerNode.parentNode : document.querySelector('[magic_design]');
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      magic_design: ""
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "designer-wrap"
    }, /*#__PURE__*/_react["default"].createElement(_PageCompList["default"], {
      pageData: MPageData,
      setPageData: this.setPageData,
      renderPagesManage: pagesManager,
      onAdd: this.onAdd,
      pageName: pageName
    }), /*#__PURE__*/_react["default"].createElement(_CompListPanel["default"], {
      ref: this.compListRef,
      onClose: this.closeCompList,
      compSource: compSource,
      onAdd: function onAdd(data) {
        return _this.addComponent(data);
      }
    }), /*#__PURE__*/_react["default"].createElement(_ConfigPanel["default"], {
      pageData: MPageData,
      ref: this.configRef,
      device: device,
      onClose: this.onPropsPanelClose
    }), /*#__PURE__*/_react["default"].createElement(_ViewArea["default"], {
      ref: this.viewAreaRef,
      device: device,
      pageData: MPageData,
      renderHeader: this.renderHeader
    })))));
  };

  return Designer;
}(_react.Component);

var _default = Designer;
exports["default"] = _default;