var _createReducer;
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); }
import { map, flatMap } from "lodash";
import { createReducer } from "redux-act";
import createAction from "./utils/createMetaAction";
import { removeItem } from "../utils/arrayUtils";
import immer from "immer";

//manages which tab panels are shown in the editor

// ------------------------------------
// Actions
// ------------------------------------
export var panelsShownUpdate = createAction("PANELS_SHOWN_UPDATE");
export var addPanelIfItDoesntAlreadyExist = createAction("addPanelIfItDoesntAlreadyExist");
export var togglePanelFullScreen = createAction("togglePanelFullScreen");
export var collapsePanel = createAction("collapsePanel");
export var closePanel = createAction("closePanel");
export var _collapseSplitScreen = createAction("_collapseSplitScreen");
export var setPanelAsActive = createAction("setPanelAsActive");
export var flipActiveTabFromLinearOrCircularIfNecessary = createAction("flipActiveTabFromLinearOrCircularIfNecessary");
export var expandTabToSplitScreen = createAction("expandTabToSplitScreen");
export var propertiesViewOpen = function propertiesViewOpen(unused, meta) {
  return setPanelAsActive("properties", meta);
};
export var createNewDigest = function createNewDigest(unused, meta) {
  return function (dispatch) {
    dispatch(addPanelIfItDoesntAlreadyExist({
      id: "digestTool",
      name: "模拟酶切",
      active: true,
      canClose: true
    }, meta));
    dispatch(setPanelAsActive("digestTool", meta));
  };
};
export var createNewPCR = function createNewPCR(unused, meta) {
  return function (dispatch) {
    dispatch(addPanelIfItDoesntAlreadyExist({
      id: "pcrTool",
      name: "模拟PCR",
      active: true,
      canClose: true
    }, meta));
    dispatch(setPanelAsActive("pcrTool", meta));
  };
};
export var createNewAlignment = function createNewAlignment(payload, meta) {
  return function (dispatch) {
    dispatch(addPanelIfItDoesntAlreadyExist(_extends({
      type: "alignment",
      name: "New Alignment",
      active: true,
      canClose: true
    }, payload), meta));
    dispatch(setPanelAsActive(payload.id, meta));
  };
};
export var createNewMismatchesList = function createNewMismatchesList(payload, meta) {
  return function (dispatch) {
    dispatch(addPanelIfItDoesntAlreadyExist(_extends({
      type: "mismatches",
      name: "Mismatches",
      active: true,
      canClose: true
    }, payload), meta));
    dispatch(setPanelAsActive(payload.id, meta));
  };
};
export var collapseSplitScreen = function collapseSplitScreen(activePanelId, meta) {
  return function (dispatch) {
    dispatch(_collapseSplitScreen(activePanelId, meta));
    activePanelId && dispatch(setPanelAsActive(activePanelId, meta));
  };
};

// ------------------------------------
// Reducer
// ------------------------------------
export default createReducer((_createReducer = {}, _createReducer[addPanelIfItDoesntAlreadyExist] = function (state, panelToAdd) {
  if (!state.some(function (panelGroup) {
    return panelGroup.some(function (_ref) {
      var id = _ref.id;
      return id === panelToAdd.id;
    });
  })) {
    return state.map(function (panelGroup, index) {
      if (index === 0) return [panelToAdd].concat(panelGroup);
      return panelGroup;
    });
  }
  return state;
}, _createReducer[panelsShownUpdate] = function (state, payload) {
  return payload.filter(function (group) {
    return group.length;
  }); //filter out any empty groups
}, _createReducer[flipActiveTabFromLinearOrCircularIfNecessary] = function (state, setCircActive) {
  var newState = immer(state, function (s) {
    s.forEach(function (g) {
      flipActiveForGroup(g, setCircActive);
    });
  });
  return newState;
}, _createReducer[closePanel] = function (state, idToClose) {
  var newState = state.map(function (group) {
    var indexToClose;
    group.forEach(function (_ref2, i) {
      var id = _ref2.id;
      if (id === idToClose) indexToClose = i;
    });
    if (indexToClose > -1) {
      return removeItem(group, indexToClose).map(function (tab, i) {
        if (i === 0) return _extends({}, tab, {
          active: true
        });else {
          return tab;
        }
      });
    }
    return group;
  });
  return newState.filter(function (group) {
    return group.length;
  }); //filter out any empty groups
}, _createReducer[_collapseSplitScreen] = function (state) {
  return [flatMap(state)];
}, _createReducer[expandTabToSplitScreen] = function (state, activePanelId) {
  var panelToMove;
  return [state[0].filter(function (panel) {
    if (panel.id === activePanelId) {
      panelToMove = panel;
      return false;
    }
    return true;
  }).map(function (panel, i) {
    return i === 0 ? _extends({}, panel, {
      active: true
    }) : panel;
  }), [_extends({}, panelToMove, {
    active: true
  })]];
}, _createReducer[setPanelAsActive] = function (state, panelId) {
  return map(state, function (panelGroup) {
    var isPanelInGroup = panelGroup.some(function (_ref3) {
      var id = _ref3.id;
      return panelId === id;
    });
    return panelGroup.map(function (panel) {
      return _extends({}, panel, {
        active: panelId === panel.id ? true : isPanelInGroup ? false : panel.active
      });
    });
  });
}, _createReducer[togglePanelFullScreen] = function (state, panelId) {
  return map(state, function (panelGroup) {
    var isPanelInGroup = panelGroup.some(function (_ref4) {
      var id = _ref4.id;
      return panelId === id;
    });
    return panelGroup.map(function (panel) {
      return _extends({}, panel, {
        active: panelId === panel.id ? true : isPanelInGroup ? false : panel.active,
        fullScreen: panelId === panel.id ? !panel.fullScreen : isPanelInGroup ? false : panel.fullScreen
      });
    });
  });
}, _createReducer[collapsePanel] = function (state, panelToCloseId) {
  return [flatMap(state, function (panelGroup) {
    return panelGroup;
  }).map(function (panel) {
    if (panel.id === panelToCloseId) {
      return _extends({}, panel, {
        active: false
      });
    }
    return panel;
  })];
}, _createReducer), [[{
  id: "circular",
  name: "环形图谱",
  active: true
  //   canClose: true
}, {
  id: "rail",
  name: "线形图谱",
  active: false
}], [{
  id: "sequence",
  name: "序列图",
  active: true
}, {
  id: "properties",
  name: "特性",
  active: false
}]]);
function flipActiveForGroup(group, setCircActive) {
  var activeTab = group.find(function (_ref5) {
    var active = _ref5.active;
    return active;
  });
  if ((activeTab === null || activeTab === void 0 ? void 0 : activeTab.id) === (setCircActive ? "rail" : "circular")) {
    //we're on the wrong tab type so check if the other tab is in
    var newTabToActivate = group.find(function (_ref6) {
      var id = _ref6.id;
      return id === (setCircActive ? "circular" : "rail");
    });
    if (newTabToActivate) {
      newTabToActivate.active = true;
      activeTab.active = false;
    }
  }
}