import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread";

var _marked =
/*#__PURE__*/
_regeneratorRuntime.mark(reactOnSwitchToggle),
    _marked2 =
/*#__PURE__*/
_regeneratorRuntime.mark(applyReactionOnSwitchToggleToNamespace),
    _marked3 =
/*#__PURE__*/
_regeneratorRuntime.mark(reactOnButtonAction),
    _marked4 =
/*#__PURE__*/
_regeneratorRuntime.mark(applyReactionOnButtonActionToNamespace),
    _marked5 =
/*#__PURE__*/
_regeneratorRuntime.mark(reactOnToggledFolder),
    _marked6 =
/*#__PURE__*/
_regeneratorRuntime.mark(reactOnSourceSet),
    _marked7 =
/*#__PURE__*/
_regeneratorRuntime.mark(reactByUpdatingFoldersState),
    _marked8 =
/*#__PURE__*/
_regeneratorRuntime.mark(reactOnCcFlowEdgeSelect),
    _marked9 =
/*#__PURE__*/
_regeneratorRuntime.mark(reactBySettingActiveNamespace),
    _marked10 =
/*#__PURE__*/
_regeneratorRuntime.mark(rootSaga);

import { put, takeLatest, select, all } from 'redux-saga/effects'; // import { delay } from 'redux-saga';

import { ACTIONS as DATA_BUS_ACTIONS } from '../dataBus/constants';
import { calcFilesTreeLayoutNodes, openAllFolders, closeAllFolders, selectDependencyEdge, setDependenciesEntryPoint, selectCodeCrumbedFlow, updateFoldersByActiveChildren } from '../dataBus/actions';
import { getNamespacesList } from '../dataBus/selectors';
import { ACTIONS as SWITCHES_ACTIONS, CONTROLS_KEYS } from '../controlsBus/constants';
import { setDisabledControl, toggleSwitch } from '../controlsBus/actions';
import { getCheckedState } from '../controlsBus/selectors';
import { setActiveNamespace } from '../namespaceIntegration/actions';

function reactOnSwitchToggle(action) {
  var namespacesList, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, namespace;

  return _regeneratorRuntime.wrap(function reactOnSwitchToggle$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return select(getNamespacesList);

        case 2:
          namespacesList = _context.sent;
          _iteratorNormalCompletion = true;
          _didIteratorError = false;
          _iteratorError = undefined;
          _context.prev = 6;
          _iterator = namespacesList[Symbol.iterator]();

        case 8:
          if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
            _context.next = 15;
            break;
          }

          namespace = _step.value;
          _context.next = 12;
          return applyReactionOnSwitchToggleToNamespace(_objectSpread({}, action.payload, {
            namespace: namespace
          }));

        case 12:
          _iteratorNormalCompletion = true;
          _context.next = 8;
          break;

        case 15:
          _context.next = 21;
          break;

        case 17:
          _context.prev = 17;
          _context.t0 = _context["catch"](6);
          _didIteratorError = true;
          _iteratorError = _context.t0;

        case 21:
          _context.prev = 21;
          _context.prev = 22;

          if (!_iteratorNormalCompletion && _iterator.return != null) {
            _iterator.return();
          }

        case 24:
          _context.prev = 24;

          if (!_didIteratorError) {
            _context.next = 27;
            break;
          }

          throw _iteratorError;

        case 27:
          return _context.finish(24);

        case 28:
          return _context.finish(21);

        case 29:
        case "end":
          return _context.stop();
      }
    }
  }, _marked, this, [[6, 17, 21, 29], [22,, 24, 28]]);
}

function applyReactionOnSwitchToggleToNamespace(_ref) {
  var switchKey, checked, namespace, otherField;
  return _regeneratorRuntime.wrap(function applyReactionOnSwitchToggleToNamespace$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          switchKey = _ref.switchKey, checked = _ref.checked, namespace = _ref.namespace;

          if (!(switchKey === CONTROLS_KEYS.SOURCE_KEEP_ONLY_ACTIVE_ITEMS)) {
            _context2.next = 5;
            break;
          }

          if (!checked) {
            _context2.next = 5;
            break;
          }

          _context2.next = 5;
          return reactByUpdatingFoldersState({
            namespace: namespace
          });

        case 5:
          if (!(switchKey === CONTROLS_KEYS.CODE_CRUMBS_DIAGRAM_ON)) {
            _context2.next = 13;
            break;
          }

          if (!checked) {
            _context2.next = 11;
            break;
          }

          _context2.next = 9;
          return put(selectCodeCrumbedFlow(undefined, namespace));

        case 9:
          _context2.next = 13;
          break;

        case 11:
          _context2.next = 13;
          return reactByUpdatingFoldersState({
            namespace: namespace
          });

        case 13:
          if (!(switchKey === CONTROLS_KEYS.CODE_CRUMBS_MINIMIZE)) {
            _context2.next = 16;
            break;
          }

          _context2.next = 16;
          return all([put(setDisabledControl(CONTROLS_KEYS.CODE_CRUMBS_LINE_NUMBERS, checked)), put(setDisabledControl(CONTROLS_KEYS.CODE_CRUMBS_DETAILS, checked)), put(setDisabledControl(CONTROLS_KEYS.CODE_CRUMBS_CODE_PREVIEW, checked)), put(calcFilesTreeLayoutNodes(namespace))]);

        case 16:
          if (!(switchKey === CONTROLS_KEYS.CODE_CRUMBS_DETAILS || switchKey === CONTROLS_KEYS.CODE_CRUMBS_CODE_PREVIEW)) {
            _context2.next = 23;
            break;
          }

          _context2.next = 19;
          return all([put(calcFilesTreeLayoutNodes(namespace))]);

        case 19:
          if (!checked) {
            _context2.next = 23;
            break;
          }

          otherField = switchKey === CONTROLS_KEYS.CODE_CRUMBS_DETAILS ? CONTROLS_KEYS.CODE_CRUMBS_CODE_PREVIEW : CONTROLS_KEYS.CODE_CRUMBS_DETAILS;
          _context2.next = 23;
          return put(toggleSwitch(otherField, false));

        case 23:
          if (!(switchKey === CONTROLS_KEYS.DEPENDENCIES_DIAGRAM_ON)) {
            _context2.next = 31;
            break;
          }

          if (!checked) {
            _context2.next = 29;
            break;
          }

          _context2.next = 27;
          return put(setDependenciesEntryPoint(undefined, namespace));

        case 27:
          _context2.next = 31;
          break;

        case 29:
          _context2.next = 31;
          return reactByUpdatingFoldersState({
            namespace: namespace
          });

        case 31:
          if (!(switchKey === CONTROLS_KEYS.DEPENDENCIES_SHOW_DIRECT_ONLY)) {
            _context2.next = 34;
            break;
          }

          _context2.next = 34;
          return all([put(selectDependencyEdge(undefined, namespace)), put(setDependenciesEntryPoint(undefined, namespace))]);

        case 34:
        case "end":
          return _context2.stop();
      }
    }
  }, _marked2, this);
}

function reactOnButtonAction(action) {
  var namespacesList, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, namespace;

  return _regeneratorRuntime.wrap(function reactOnButtonAction$(_context3) {
    while (1) {
      switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 2;
          return select(getNamespacesList);

        case 2:
          namespacesList = _context3.sent;
          _iteratorNormalCompletion2 = true;
          _didIteratorError2 = false;
          _iteratorError2 = undefined;
          _context3.prev = 6;
          _iterator2 = namespacesList[Symbol.iterator]();

        case 8:
          if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) {
            _context3.next = 15;
            break;
          }

          namespace = _step2.value;
          _context3.next = 12;
          return applyReactionOnButtonActionToNamespace({
            buttonKey: action.payload,
            namespace: namespace
          });

        case 12:
          _iteratorNormalCompletion2 = true;
          _context3.next = 8;
          break;

        case 15:
          _context3.next = 21;
          break;

        case 17:
          _context3.prev = 17;
          _context3.t0 = _context3["catch"](6);
          _didIteratorError2 = true;
          _iteratorError2 = _context3.t0;

        case 21:
          _context3.prev = 21;
          _context3.prev = 22;

          if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
            _iterator2.return();
          }

        case 24:
          _context3.prev = 24;

          if (!_didIteratorError2) {
            _context3.next = 27;
            break;
          }

          throw _iteratorError2;

        case 27:
          return _context3.finish(24);

        case 28:
          return _context3.finish(21);

        case 29:
        case "end":
          return _context3.stop();
      }
    }
  }, _marked3, this, [[6, 17, 21, 29], [22,, 24, 28]]);
}

function applyReactionOnButtonActionToNamespace(_ref2) {
  var buttonKey, namespace;
  return _regeneratorRuntime.wrap(function applyReactionOnButtonActionToNamespace$(_context4) {
    while (1) {
      switch (_context4.prev = _context4.next) {
        case 0:
          buttonKey = _ref2.buttonKey, namespace = _ref2.namespace;

          if (!(buttonKey === CONTROLS_KEYS.SOURCE_EXPAND_ALL)) {
            _context4.next = 5;
            break;
          }

          _context4.next = 4;
          return all([put(openAllFolders(namespace)), put(calcFilesTreeLayoutNodes(namespace))]);

        case 4:
          return _context4.abrupt("return", _context4.sent);

        case 5:
          if (!(buttonKey === CONTROLS_KEYS.SOURCE_COLLAPSE_TO_MIN)) {
            _context4.next = 9;
            break;
          }

          _context4.next = 8;
          return all([put(closeAllFolders(namespace)), put(calcFilesTreeLayoutNodes(namespace))]);

        case 8:
          return _context4.abrupt("return", _context4.sent);

        case 9:
        case "end":
          return _context4.stop();
      }
    }
  }, _marked4, this);
}

function reactOnToggledFolder(_ref3) {
  var namespace;
  return _regeneratorRuntime.wrap(function reactOnToggledFolder$(_context5) {
    while (1) {
      switch (_context5.prev = _context5.next) {
        case 0:
          namespace = _ref3.namespace;
          _context5.next = 3;
          return put(calcFilesTreeLayoutNodes(namespace));

        case 3:
        case "end":
          return _context5.stop();
      }
    }
  }, _marked5, this);
}

function reactOnSourceSet(_ref4) {
  var namespace, _ref5, dependenciesDiagramOn, codeCrumbsDiagramOn;

  return _regeneratorRuntime.wrap(function reactOnSourceSet$(_context6) {
    while (1) {
      switch (_context6.prev = _context6.next) {
        case 0:
          namespace = _ref4.namespace;
          _context6.next = 3;
          return select(getCheckedState);

        case 3:
          _ref5 = _context6.sent;
          dependenciesDiagramOn = _ref5.dependenciesDiagramOn;
          codeCrumbsDiagramOn = _ref5.codeCrumbsDiagramOn;
          _context6.next = 8;
          return put(setActiveNamespace(namespace));

        case 8:
          if (!(!dependenciesDiagramOn && !codeCrumbsDiagramOn)) {
            _context6.next = 11;
            break;
          }

          _context6.next = 11;
          return reactByUpdatingFoldersState({
            namespace: namespace
          });

        case 11:
          if (!dependenciesDiagramOn) {
            _context6.next = 14;
            break;
          }

          _context6.next = 14;
          return put(setDependenciesEntryPoint(undefined, namespace));

        case 14:
          if (!codeCrumbsDiagramOn) {
            _context6.next = 17;
            break;
          }

          _context6.next = 17;
          return put(selectCodeCrumbedFlow(undefined, namespace));

        case 17:
        case "end":
          return _context6.stop();
      }
    }
  }, _marked6, this);
}

function reactByUpdatingFoldersState(_ref6) {
  var namespace;
  return _regeneratorRuntime.wrap(function reactByUpdatingFoldersState$(_context7) {
    while (1) {
      switch (_context7.prev = _context7.next) {
        case 0:
          namespace = _ref6.namespace;
          _context7.next = 3;
          return put(updateFoldersByActiveChildren(namespace));

        case 3:
          _context7.next = 5;
          return put(calcFilesTreeLayoutNodes(namespace));

        case 5:
        case "end":
          return _context7.stop();
      }
    }
  }, _marked7, this);
} // TODO: will work if switches per namespace as well

/*function* reactByDisablingFeatures({ payload }) {
  const { fullFeaturesSupport } = yield select(state =>
    getProjectMetadata(state, { namespace: payload })
  );
  const { dependenciesDiagramOn } = yield select(getCheckedState);

  if (!fullFeaturesSupport && dependenciesDiagramOn) {
    yield put(toggleSwitch(CONTROLS_KEYS.DEPENDENCIES_DIAGRAM_ON, false));
  }
}*/


function reactOnCcFlowEdgeSelect(_ref7) {
  var payload, namespace;
  return _regeneratorRuntime.wrap(function reactOnCcFlowEdgeSelect$(_context8) {
    while (1) {
      switch (_context8.prev = _context8.next) {
        case 0:
          payload = _ref7.payload, namespace = _ref7.namespace;

          if (!(payload && payload.source.namespace === namespace)) {
            _context8.next = 4;
            break;
          }

          _context8.next = 4;
          return put(setActiveNamespace(namespace));

        case 4:
        case "end":
          return _context8.stop();
      }
    }
  }, _marked8, this);
}

function reactBySettingActiveNamespace(_ref8) {
  var payload, namespace;
  return _regeneratorRuntime.wrap(function reactBySettingActiveNamespace$(_context9) {
    while (1) {
      switch (_context9.prev = _context9.next) {
        case 0:
          payload = _ref8.payload, namespace = _ref8.namespace;
          _context9.next = 3;
          return put(setActiveNamespace(namespace));

        case 3:
        case "end":
          return _context9.stop();
      }
    }
  }, _marked9, this);
}

export default function rootSaga() {
  return _regeneratorRuntime.wrap(function rootSaga$(_context10) {
    while (1) {
      switch (_context10.prev = _context10.next) {
        case 0:
          _context10.next = 2;
          return all([takeLatest(SWITCHES_ACTIONS.TOGGLE_SWITCH, reactOnSwitchToggle), takeLatest(SWITCHES_ACTIONS.FIRE_BUTTON_ACTION, reactOnButtonAction), takeLatest(DATA_BUS_ACTIONS.TOGGLE_FOLDER, reactOnToggledFolder), takeLatest(DATA_BUS_ACTIONS.SET_INITIAL_SOURCE_DATA, reactOnSourceSet), takeLatest(DATA_BUS_ACTIONS.SET_CHANGED_SOURCE_DATA, reactOnSourceSet), takeLatest(DATA_BUS_ACTIONS.SET_DEPENDENCIES_ENTRY_POINT, reactByUpdatingFoldersState), takeLatest(DATA_BUS_ACTIONS.SELECT_CODE_CRUMBED_FLOW, reactByUpdatingFoldersState), takeLatest(DATA_BUS_ACTIONS.UPDATE_FILES, reactByUpdatingFoldersState), takeLatest(DATA_BUS_ACTIONS.SELECT_CC_FLOW_EDGE, reactOnCcFlowEdgeSelect), takeLatest(DATA_BUS_ACTIONS.SELECT_NODE, reactBySettingActiveNamespace), takeLatest(DATA_BUS_ACTIONS.SELECT_CODE_CRUMB, reactBySettingActiveNamespace)]);

        case 2:
        case "end":
          return _context10.stop();
      }
    }
  }, _marked10, this);
}