"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SlotParser = SlotParser;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _SlotContainer = _interopRequireDefault(require("./SlotContainer"));
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _type = require("./type");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
//
const {
  width: windowWidth,
  height: windowHeight
} = _reactNative.Dimensions.get('window');
const SlotC = /*#__PURE__*/(0, _react.forwardRef)(function SlotC(props, ref) {
  const {
    children,
    onLayout,
    style,
    width,
    height
  } = props;

  // TODO： 重构样式计算方式，支持更多的样式配置写入
  const styles = _reactNative.StyleSheet.create({
    container: {
      flex: 1,
      position: 'absolute',
      maxHeight: windowHeight * 0.5,
      height: windowHeight * height,
      width: windowWidth * width,
      maxWidth: windowWidth * 0.4,
      flexDirection: 'column',
      ...style
    },
    scrollView: {
      flex: 1,
      flexDirection: 'column'
    }
  });
  const handleLayoutChange = event => {
    // const { height } = event.nativeEvent.layout;
    // setBottomCenterHeight(height);
    if (onLayout) {
      onLayout(event);
    }
  };
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: styles.container,
    onLayout: handleLayoutChange,
    ref: ref
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.scrollView
  }, children));
});
var SlotLayoutReducerActionEnum = /*#__PURE__*/function (SlotLayoutReducerActionEnum) {
  SlotLayoutReducerActionEnum["update"] = "update";
  return SlotLayoutReducerActionEnum;
}(SlotLayoutReducerActionEnum || {});
function getDefaultLayout(slots) {
  const result = {
    [_type.SlotTypeEnum.bottomCenter]: {
      width: 0,
      height: 0,
      ...(slots[_type.SlotTypeEnum.bottomCenter]?.style || {})
    },
    [_type.SlotTypeEnum.leftBottom]: {
      width: 0,
      height: 0,
      ...(slots[_type.SlotTypeEnum.leftBottom]?.style || {})
    },
    [_type.SlotTypeEnum.leftTop]: {
      width: 0,
      height: 0,
      ...(slots[_type.SlotTypeEnum.leftTop]?.style || {})
    },
    [_type.SlotTypeEnum.rightBottom]: {
      width: 0,
      height: 0,
      ...(slots[_type.SlotTypeEnum.rightBottom]?.style || {})
    },
    [_type.SlotTypeEnum.rightTop]: {
      width: 0,
      height: 0,
      ...(slots[_type.SlotTypeEnum.rightTop]?.style || {})
    },
    [_type.SlotTypeEnum.topCenter]: {
      width: 0,
      height: 0,
      ...(slots[_type.SlotTypeEnum.topCenter]?.style || {})
    }
  };
  return result;
}
function renderInfoReducer(store, action) {
  const {
    type,
    payload
  } = action;
  const {
    slotType,
    ...newLayout
  } = payload;
  const oldLayout = store[slotType];
  switch (type) {
    case SlotLayoutReducerActionEnum.update:
      break;
    //
  }
  store[slotType] = {
    ...oldLayout,
    ...newLayout
  };
  return store;
}

// function slotLayoutTransform() {
//     const layout = {
//         width: 0,
//         height: 0,
//     };

//     return layout;
// }

function SlotParser(props) {
  function pickParams(slots, type, renderInfo) {
    const info = slots[type];
    // const ren = renderInfo[type];
    // const style = StyleSheet.create({
    //     root: { ...info.style },
    // });

    return {
      key: type,
      type,
      style: {
        ...info.style
      },
      visible: info.visible
    };
  }
  const {
    slots
  } = props;
  const [renderInfo, dispatchRenderInfo] = (0, _react.useReducer)(renderInfoReducer, getDefaultLayout(slots));
  const handleBottomCenterLayout = event => {
    // 1、当 bottomCenter 布局出现变化时，计算leftBottom、rightBottom的位置变更！！！
    updateSlotLayout(_type.SlotTypeEnum.bottomCenter)(event);

    // 1、触发更新 rightBottom, leftBottom 位置
    const leftBottomSlotLayout = renderInfo[_type.SlotTypeEnum.leftBottom];
    const rightBottomSlotLayout = renderInfo[_type.SlotTypeEnum.rightBottom];
    const {
      height
    } = event.nativeEvent.layout;
    //

    const expectLTSTop = height + leftBottomSlotLayout.height;
    if (expectLTSTop < windowHeight) {
      if (expectLTSTop > (leftBottomSlotLayout.top || 0)) {
        dispatchRenderInfo({
          type: SlotLayoutReducerActionEnum.update,
          payload: {
            slotType: _type.SlotTypeEnum.leftBottom,
            top: windowHeight - expectLTSTop
          }
        });
      }
    } else {
      dispatchRenderInfo({
        type: SlotLayoutReducerActionEnum.update,
        payload: {
          slotType: _type.SlotTypeEnum.leftBottom,
          visible: false
        }
      });
    }

    // 更新 rightBottom
    const expectRTSTop = height + rightBottomSlotLayout.height;
    if (expectRTSTop < windowHeight) {
      if (expectRTSTop > (rightBottomSlotLayout.top || 0)) {
        dispatchRenderInfo({
          type: SlotLayoutReducerActionEnum.update,
          payload: {
            slotType: _type.SlotTypeEnum.rightBottom,
            top: windowHeight - expectRTSTop
          }
        });
      }
    } else {
      dispatchRenderInfo({
        type: SlotLayoutReducerActionEnum.update,
        payload: {
          slotType: _type.SlotTypeEnum.rightBottom,
          visible: false
        }
      });
    }
  };

  // 当Slot内容更新时， onLayout 触发
  const updateSlotLayout = type => e => {
    const {
      width,
      height
    } = e.nativeEvent.layout;
    dispatchRenderInfo({
      type: SlotLayoutReducerActionEnum.update,
      payload: {
        slotType: type,
        width,
        height
      }
    });
  };

  // version first
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(SlotC, _extends({
    width: 0.3,
    height: 0.3
  }, pickParams(slots, _type.SlotTypeEnum.leftTop, renderInfo), {
    onLayout: updateSlotLayout(_type.SlotTypeEnum.leftTop)
  }), slots[_type.SlotTypeEnum.leftTop].child), /*#__PURE__*/_react.default.createElement(SlotC, _extends({
    width: 0.3,
    height: 0.3
  }, pickParams(slots, _type.SlotTypeEnum.rightTop, renderInfo), {
    onLayout: updateSlotLayout(_type.SlotTypeEnum.rightTop)
  }), slots[_type.SlotTypeEnum.rightTop].child), /*#__PURE__*/_react.default.createElement(SlotC, _extends({
    width: 0.3,
    height: 0.3
  }, pickParams(slots, _type.SlotTypeEnum.leftBottom, renderInfo), {
    onLayout: updateSlotLayout(_type.SlotTypeEnum.leftBottom)
  }), slots[_type.SlotTypeEnum.leftBottom].child), /*#__PURE__*/_react.default.createElement(SlotC, _extends({
    width: 0,
    height: 0
  }, pickParams(slots, _type.SlotTypeEnum.rightBottom, renderInfo), {
    onLayout: updateSlotLayout(_type.SlotTypeEnum.rightBottom)
  }), slots[_type.SlotTypeEnum.rightBottom].child), /*#__PURE__*/_react.default.createElement(SlotC, _extends({
    width: 1,
    height: 0.3
  }, pickParams(slots, _type.SlotTypeEnum.bottomCenter, renderInfo), {
    onLayout: handleBottomCenterLayout
  }), slots[_type.SlotTypeEnum.bottomCenter].child));
}
const ParentComponent = ({
  children
}) => {
  return /*#__PURE__*/_react.default.createElement(_SlotContainer.default, null, children);
};
var _default = exports.default = ParentComponent;
//# sourceMappingURL=ParentComponent.js.map