import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import './index-fe887b5f.js';
import DivaUtils from '@crystaldesign/diva-utils';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/regenerator';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/toConsumableArray';
import 'axios';
import '@crystaldesign/basket-model';
import '@crystaldesign/basket-handler';
import 'socket.io-client';
import '@babel/runtime/helpers/typeof';

var Door = /*#__PURE__*/function () {
  function Door(type, basketId, index) {
    _classCallCheck(this, Door);
    _defineProperty(this, "id", void 0);
    _defineProperty(this, "type", void 0);
    _defineProperty(this, "isReplaced", void 0);
    _defineProperty(this, "basketId", void 0);
    _defineProperty(this, "index", void 0);
    this.type = type;
    this.id = basketId + '_' + index;
    this.basketId = basketId;
    this.index = index;
  }
  return _createClass(Door, [{
    key: "move",
    value: function move(door) {
      if (door.id == this.id) {
        //if a door is moved back to its original place
        this.isReplaced = undefined;
      } else {
        this.isReplaced = door;
      }
    }
  }, {
    key: "reset",
    value: function reset() {
      this.isReplaced = undefined;
    }
  }, {
    key: "StrictType",
    get: function get() {
      if (this.type.includes('TUER_L_SHORT')) {
        return 'TUER_L_SHORT';
      }
      if (this.type.includes('TUER_R_SHORT')) {
        return 'TUER_R_SHORT';
      }
      if (this.type.includes('TUER_L')) {
        return 'TUER_L';
      }
      if (this.type.includes('TUER_R')) {
        return 'TUER_R';
      }
      return 'TUER_NONE';
    }
  }, {
    key: "Type",
    get: function get() {
      return this.type;
    }
  }, {
    key: "IsReplaced",
    get: function get() {
      return this.isReplaced;
    }
  }, {
    key: "currentDoor",
    get: function get() {
      var _this$IsReplaced;
      return (_this$IsReplaced = this.IsReplaced) !== null && _this$IsReplaced !== void 0 ? _this$IsReplaced : this;
    }
  }, {
    key: "BasketId",
    get: function get() {
      return this.basketId;
    }
  }]);
}();

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; }
var DoorPlanner = /*#__PURE__*/function () {
  function DoorPlanner() {
    _classCallCheck(this, DoorPlanner);
    _defineProperty(this, "doorsOnArticles", {});
    _defineProperty(this, "doorsSwitcher", {});
    _defineProperty(this, "activeIndexOfDoors", {});
    _defineProperty(this, "divaArticles", {});
    _defineProperty(this, "onChange", function (movedDoor, targetElement) {
      var currentMovedDoor = movedDoor.currentDoor;
      movedDoor.move(targetElement.currentDoor);
      targetElement.move(currentMovedDoor);
    });
  }
  return _createClass(DoorPlanner, [{
    key: "initializeArticles",
    value: function initializeArticles(data, article) {
      var basketId = article.basketId;
      this.divaArticles[basketId] = article;
      if (data.DoorsSwitcher) {
        this.doorsSwitcher[article.basketId] = data.DoorsSwitcher;
      } else if (data.Doors) {
        this.doorsSwitcher[article.basketId] = [{
          Doors: data.Doors,
          DoShowDoorsOnRule: data.DoShowDoorsOnRule
        }];
      }
    }
  }, {
    key: "initDoors",
    value: function initDoors(article, doorsSwitcher) {
      this.doorsOnArticles[article.basketId] = [];
      for (var i = 0; i < doorsSwitcher.Doors.length; i++) {
        this.doorsOnArticles[article.basketId].push(new Door(doorsSwitcher.Doors[i], article.basketId, i));
      }
    }
  }, {
    key: "alreadyInitialized",
    value: function alreadyInitialized(basketId) {
      return !!this.divaArticles[basketId];
    }
  }, {
    key: "initDoorPlanner",
    value: function initDoorPlanner() {
      var _this = this;
      var _loop = function _loop() {
        var article = _this.divaArticles[key];
        if (article.doorPlannerData) {
          _this.updateAfterConfigChanged();
          var _loop2 = function _loop2(i) {
            var targetArticle = Object.values(_this.divaArticles).find(function (art) {
              return 'E' + art.elementId == article.doorPlannerData[i].toElement;
            });
            var targetDoor = _this.doorsOnArticles[targetArticle.basketId].find(function (door) {
              return door.Type === article.doorPlannerData[i].toType;
            });
            var insertingDoor = _this.doorsOnArticles[key].find(function (door) {
              return door.Type === article.doorPlannerData[i].Type;
            });
            if (targetDoor && insertingDoor) {
              targetDoor.move(insertingDoor);
            }
          };
          for (var i = 0; i < article.doorPlannerData.length; i++) {
            _loop2(i);
          }
          article.doorPlannerData = undefined;
        }
      };
      for (var key in this.divaArticles) {
        _loop();
      }
    }
  }, {
    key: "isVisible",
    get: function get() {
      for (var key in this.activeIndexOfDoors) {
        var _this$doorsSwitcher$k;
        var rule = (_this$doorsSwitcher$k = this.doorsSwitcher[key][this.activeIndexOfDoors[key]]) === null || _this$doorsSwitcher$k === void 0 ? void 0 : _this$doorsSwitcher$k.DoShowDoorsOnRule;
        if (!rule || this.divaArticles[key].evalRule(DivaUtils.getRuleByFormula(rule, this.divaArticles[key].catalogCodex, this.divaArticles[key].CatalogData.multiselectOptions))) {
          return true;
        }
      }
      return false;
    }
  }, {
    key: "updateAfterConfigChanged",
    value: function updateAfterConfigChanged() {
      var somethingChanged = false;
      for (var key in this.doorsSwitcher) {
        var article = this.divaArticles[key];
        var doors = this.doorsSwitcher[key];
        var newActiveIndex = 0; // firt entry is default if there is no active rule

        for (var i = 0; i < doors.length; i++) {
          var d = doors[i];
          if (!d.DoShowDoorsOnRule || article.evalRule(DivaUtils.getRuleByFormula(d.DoShowDoorsOnRule, article.catalogCodex, article.CatalogData.multiselectOptions))) {
            newActiveIndex = i;
            break;
          }
        }
        if (newActiveIndex >= 0 && this.activeIndexOfDoors[key] != newActiveIndex) {
          somethingChanged = true;
          this.initDoors(article, doors[newActiveIndex]);
          this.activeIndexOfDoors[key] = newActiveIndex;
        }
      }
      if (somethingChanged) {
        this.resetDoorplanner();
      }
    }
  }, {
    key: "deleteElement",
    value: function deleteElement(basketId) {
      delete this.divaArticles[basketId];
      delete this.doorsSwitcher[basketId];
      delete this.doorsOnArticles[basketId];
      delete this.activeIndexOfDoors[basketId];
      this.resetDoorplanner();
    }
  }, {
    key: "resetDoorplanner",
    value: function resetDoorplanner() {
      for (var key in this.doorsOnArticles) {
        for (var i = 0; i < this.doorsOnArticles[key].length; i++) {
          this.doorsOnArticles[key][i].reset();
        }
      }
    }
  }, {
    key: "getMovedData",
    value: function getMovedData(basketId) {
      var _this2 = this;
      var ret = [];
      var _loop3 = function _loop3() {
        var door = _this2.doorsOnArticles[basketId][i];
        for (var key in _this2.doorsOnArticles) {
          var foundDoor = _this2.doorsOnArticles[key].find(function (doorItem) {
            return doorItem.currentDoor.Type == door.Type && doorItem.currentDoor.BasketId === door.BasketId;
          });
          if (foundDoor && door.IsReplaced) {
            ret.push({
              Type: door.Type,
              toElement: 'E' + _this2.divaArticles[foundDoor.BasketId].elementId,
              toType: foundDoor.Type
            });
          }
        }
      };
      for (var i = 0; i < ((_this$doorsOnArticles = (_this$doorsOnArticles2 = this.doorsOnArticles[basketId]) === null || _this$doorsOnArticles2 === void 0 ? void 0 : _this$doorsOnArticles2.length) !== null && _this$doorsOnArticles !== void 0 ? _this$doorsOnArticles : 0); i++) {
        var _this$doorsOnArticles, _this$doorsOnArticles2;
        _loop3();
      }
      return ret.length ? ret : undefined;
    }
  }, {
    key: "getDoorPlannerItems",
    value: function getDoorPlannerItems() {
      var ret = [];
      for (var basketId in this.doorsOnArticles) {
        for (var i = 0; i < this.doorsOnArticles[basketId].length; i++) {
          var door = this.doorsOnArticles[basketId][i];
          ret.push({
            key: door.Type + door.BasketId,
            currentDoor: door.currentDoor,
            type: door.StrictType,
            door: door
          });
        }
      }
      return ret;
    }
  }, {
    key: "DoorsOnArticles",
    get: function get() {
      var ret = {};
      for (var key in this.doorsOnArticles) {
        ret[key] = _objectSpread(_objectSpread({}, this.doorsOnArticles[key]), {}, {
          elementId: this.divaArticles[key].elementId
        });
      }
      return ret;
    }
  }]);
}();

export { DoorPlanner as default };
