"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _map = _interopRequireDefault(require("../map"));
var _excluded = ["children", "wrapStyle", "createdMap"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var Context = /*#__PURE__*/_react["default"].createContext({});
var Provider = Context.Provider;
var Item = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var children = props.children,
    wrapStyle = props.wrapStyle,
    createdMapProps = props.createdMap,
    otherVal = _objectWithoutProperties(props, _excluded);
  var dom = (0, _react.useRef)();
  var mapRef = (0, _react.useRef)();
  var _useContext = (0, _react.useContext)(Context),
    addMap = _useContext.addMap,
    removeMap = _useContext.removeMap;

  // 移除地图
  (0, _react.useEffect)(function () {
    return function () {
      removeMap(mapRef.current);
    };
  }, []);

  // 暴露给父组件
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      dom: dom.current,
      mapRef: mapRef.current
    };
  });

  // 创建地图
  var createdMap = function createdMap(map) {
    mapRef.current = map;
    createdMapProps === null || createdMapProps === void 0 || createdMapProps(map);
    addMap(map);
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: dom,
    style: _objectSpread({
      flex: 1,
      overflow: "hidden",
      border: '1px solid #ccc'
    }, wrapStyle)
  }, /*#__PURE__*/_react["default"].createElement(_map["default"], _extends({}, otherVal, {
    createdMap: createdMap
  }), children));
});
var SplitScreen = function SplitScreen(props) {
  var mode = props.mode,
    children = props.children,
    style = props.style;
  var mapListRef = (0, _react.useRef)([]);
  var addMap = function addMap(map) {
    mapListRef.current.push(map);
    onChange();
  };
  var removeMap = function removeMap(map) {
    mapListRef.current = mapListRef.current.filter(function (item) {
      return item !== map;
    });
    onChange();
  };
  var onChange = function onChange() {
    var currentViewer;
    // 同步视角
    var syncViewer = function syncViewer(e) {
      var _currentViewer, _e$_view;
      if (currentViewer && ((_currentViewer = currentViewer) === null || _currentViewer === void 0 || (_currentViewer = _currentViewer.scene) === null || _currentViewer === void 0 ? void 0 : _currentViewer.id) !== (e === null || e === void 0 ? void 0 : e.id) && (_e$_view = e._view) !== null && _e$_view !== void 0 && _e$_view.camera) {
        e._view.camera.flyTo({
          destination: currentViewer.camera.position,
          orientation: {
            heading: currentViewer.camera.heading,
            pitch: currentViewer.camera.pitch,
            roll: currentViewer.camera.roll
          },
          duration: 0.0
        });
      }
    };
    mapListRef.current.map(function (viewer) {
      viewer.onMouseMove = function () {
        currentViewer = viewer;
      };

      // 先移除监听
      viewer.camera.changed.removeEventListener(syncViewer);
      viewer.scene.preRender.removeEventListener(syncViewer);

      // 再添加监听
      viewer.camera.changed.addEventListener(syncViewer);
      viewer.scene.preRender.addEventListener(syncViewer);
    });
  };
  return /*#__PURE__*/_react["default"].createElement(Provider, {
    value: {
      mode: mode,
      mapList: mapListRef.current,
      addMap: addMap,
      removeMap: removeMap
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    style: _objectSpread({
      width: '100%',
      height: '100%',
      display: 'flex',
      flexDirection: mode === "VERTICAL" ? 'column' : 'row'
    }, style)
  }, children));
};
SplitScreen.Item = Item;
var _default = exports["default"] = SplitScreen;
//# sourceMappingURL=index.js.map
