import { __spreadArray, __assign } from 'tslib';
import { EventGroup, getId } from '@uifabric-vue/utilities';
import { KeytipEvents } from './KeytipConstants.js';

var KeytipManager = function() {
  function KeytipManager2() {
    this.keytips = {};
    this.persistedKeytips = {};
    this.sequenceMapping = {};
    this.inKeytipMode = false;
    this.shouldEnterKeytipMode = true;
    this.delayUpdatingKeytipChange = false;
  }
  KeytipManager2.getInstance = function() {
    return this._instance;
  };
  KeytipManager2.prototype.init = function(delayUpdatingKeytipChange) {
    this.delayUpdatingKeytipChange = delayUpdatingKeytipChange;
  };
  KeytipManager2.prototype.register = function(keytipProps, persisted) {
    if (persisted === void 0) {
      persisted = false;
    }
    var props = keytipProps;
    if (!persisted) {
      props = this.addParentOverflow(keytipProps);
      this.sequenceMapping[props.keySequences.toString()] = props;
    }
    var uniqueKeytip = this._getUniqueKtp(props);
    persisted ? this.persistedKeytips[uniqueKeytip.uniqueID] = uniqueKeytip : this.keytips[uniqueKeytip.uniqueID] = uniqueKeytip;
    if (this.inKeytipMode || !this.delayUpdatingKeytipChange) {
      var event_1 = persisted ? KeytipEvents.PERSISTED_KEYTIP_ADDED : KeytipEvents.KEYTIP_ADDED;
      EventGroup.raise(this, event_1, {
        keytip: props,
        uniqueID: uniqueKeytip.uniqueID
      });
    }
    return uniqueKeytip.uniqueID;
  };
  KeytipManager2.prototype.update = function(keytipProps, uniqueID) {
    var newKeytipProps = this.addParentOverflow(keytipProps);
    var uniqueKeytip = this._getUniqueKtp(newKeytipProps, uniqueID);
    var oldKeyTip = this.keytips[uniqueID];
    if (oldKeyTip) {
      uniqueKeytip.keytip.visible = oldKeyTip.keytip.visible;
      this.keytips[uniqueID] = uniqueKeytip;
      delete this.sequenceMapping[oldKeyTip.keytip.keySequences.toString()];
      this.sequenceMapping[uniqueKeytip.keytip.keySequences.toString()] = uniqueKeytip.keytip;
      if (this.inKeytipMode || !this.delayUpdatingKeytipChange) {
        EventGroup.raise(this, KeytipEvents.KEYTIP_UPDATED, {
          keytip: uniqueKeytip.keytip,
          uniqueID: uniqueKeytip.uniqueID
        });
      }
    }
  };
  KeytipManager2.prototype.unregister = function(keytipToRemove, uniqueID, persisted) {
    if (persisted === void 0) {
      persisted = false;
    }
    persisted ? delete this.persistedKeytips[uniqueID] : delete this.keytips[uniqueID];
    !persisted && delete this.sequenceMapping[keytipToRemove.keySequences.toString()];
    var event = persisted ? KeytipEvents.PERSISTED_KEYTIP_REMOVED : KeytipEvents.KEYTIP_REMOVED;
    if (this.inKeytipMode || !this.delayUpdatingKeytipChange) {
      EventGroup.raise(this, event, {
        keytip: keytipToRemove,
        uniqueID: uniqueID
      });
    }
  };
  KeytipManager2.prototype.enterKeytipMode = function() {
    EventGroup.raise(this, KeytipEvents.ENTER_KEYTIP_MODE);
  };
  KeytipManager2.prototype.exitKeytipMode = function() {
    EventGroup.raise(this, KeytipEvents.EXIT_KEYTIP_MODE);
  };
  KeytipManager2.prototype.getKeytips = function() {
    var _this = this;
    return Object.keys(this.keytips).map(function(key) {
      return _this.keytips[key].keytip;
    });
  };
  KeytipManager2.prototype.addParentOverflow = function(keytipProps) {
    var fullSequence = __spreadArray([], keytipProps.keySequences, true);
    fullSequence.pop();
    if (fullSequence.length !== 0) {
      var parentKeytip = this.sequenceMapping[fullSequence.toString()];
      if (parentKeytip && parentKeytip.overflowSetSequence) {
        return __assign(__assign({}, keytipProps), { overflowSetSequence: parentKeytip.overflowSetSequence });
      }
    }
    return keytipProps;
  };
  KeytipManager2.prototype.menuExecute = function(overflowButtonSequences, keytipSequences) {
    EventGroup.raise(this, KeytipEvents.PERSISTED_KEYTIP_EXECUTE, {
      overflowButtonSequences: overflowButtonSequences,
      keytipSequences: keytipSequences
    });
  };
  KeytipManager2.prototype._getUniqueKtp = function(keytipProps, uniqueID) {
    if (uniqueID === void 0) {
      uniqueID = getId();
    }
    return { keytip: __assign({}, keytipProps), uniqueID: uniqueID };
  };
  KeytipManager2._instance = new KeytipManager2();
  return KeytipManager2;
}();

export { KeytipManager };
