"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultSlotConfig = exports.default = exports.SlotActionEnum = void 0;
exports.slotReducer = slotReducer;
var _react = require("react");
var _type = require("./type");
var _common = require("./common");
const defaultSlotConfig = exports.defaultSlotConfig = {
  [_type.SlotTypeEnum.leftBottom]: {
    style: {
      left: 4,
      bottom: 4,
      backgroundColor: 'green'
    },
    componentList: [],
    child: [],
    visible: false
  },
  [_type.SlotTypeEnum.rightBottom]: {
    style: {
      right: 4,
      bottom: 4,
      backgroundColor: 'green'
    },
    visible: false,
    componentList: [],
    child: []
  },
  [_type.SlotTypeEnum.rightTop]: {
    style: {
      top: 4,
      right: 4
    },
    visible: false,
    componentList: [],
    child: []
  },
  [_type.SlotTypeEnum.leftTop]: {
    style: {
      left: 4,
      top: 4
    },
    visible: false,
    componentList: [],
    child: []
  },
  [_type.SlotTypeEnum.bottomCenter]: {
    style: {
      left: 0,
      top: 0,
      backgroundColor: 'red'
    },
    visible: false,
    componentList: [],
    child: []
  },
  [_type.SlotTypeEnum.topCenter]: {
    style: {
      left: 0,
      top: 0
    },
    visible: false,
    componentList: [],
    child: []
  }
};
const SlotContext = /*#__PURE__*/(0, _react.createContext)({
  registerSlot: function (site, key, child, options) {
    throw new Error('Function not implemented.');
  },
  removeComponent: function (site, key) {
    throw new Error('Function not implemented.');
  },
  changeComponentVisible: function (site, key, visible) {
    throw new Error('Function not implemented.');
  }
});
var _default = exports.default = SlotContext;
let SlotActionEnum = exports.SlotActionEnum = /*#__PURE__*/function (SlotActionEnum) {
  SlotActionEnum["registerSlot"] = "registerSlot";
  SlotActionEnum["removeComponent"] = "removeComponent";
  SlotActionEnum["changeComponentVisible"] = "changeComponentVisible";
  return SlotActionEnum;
}({});
;

// TODO
function slotReducer(store, action) {
  const {
    type,
    payload
  } = action;
  switch (type) {
    case SlotActionEnum.registerSlot:
      /**
       * 1、检查当前插槽上是否存在
       * 2、
       */
      const {
        site,
        key,
        children,
        options
      } = payload;
      const slot = site && store[site];
      const componentList = slot?.componentList;
      if (componentList) {
        const isExist = !!componentList.find(item => {
          return item.key === key;
        });
        if (!isExist) {
          // 默认排队在尾部
          const injectSite = options?.start ? 'start' : 'end';
          const injectInfo = {
            key: key ? key : (0, _common.generateUUID)(),
            visible: true,
            children: children
          };

          // 修改默认插槽渲染条件
          if (!slot.visible && !slot.componentList.length) {
            slot.visible = true;
          }

          // 组件插入插槽中
          // 目前仅支持从头插入、从尾部插入，不支持顺序设定！
          if (injectSite === 'start') {
            componentList.unshift(injectInfo);
          } else {
            componentList.push(injectInfo);
          }

          // 更新render child
          const renderList = [];
          componentList.forEach(item => {
            if (item.visible) {
              renderList.push(item.children);
            }
          });
          slot.child = renderList;
        }
      }
      break;
    case SlotActionEnum.changeComponentVisible:
      break;
    case SlotActionEnum.removeComponent:
      break;
  }
  return store;
}
//# sourceMappingURL=slotContext.js.map