var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { normalizeNumberByRanger } from '../../../../utils/number';
import { Observer } from '../../../../helpers/change-manager/observer/observer';
import changeManager from '../../../../helpers/change-manager/change-manager';
import domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalProgressTrackerStepClick } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { SynchronizeTaskQueue } from '../../../../helpers/queue/synchronize-task-queue';
/**
 * Controls the current step of the progress tracker component.
 */
export class VegaProgressTrackerCurrentStepController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.goToPageTaskQueue = new SynchronizeTaskQueue();
    /**
     * Update current step with range limit
     *
     * @param {number} current - current step index
     */
    this.updateCurrent = (current) => {
      const maxIndex = Math.max(0, this.currentSteps.length - 1);
      const minIndex = this.allowNoCurrentStep ? -1 : 0;
      this.current = normalizeNumberByRanger(current, minIndex, maxIndex);
    };
  }
  /** connectedCallback */
  connectedCallback() {
    this.initStepClickObserver();
  }
  /** disconnectedCallback */
  disconnectedCallback() {
    this.removeStepClickObserver();
  }
  /** componentDidLoad */
  componentDidLoad() {
    this.updateCurrent(this.current);
  }
  /** componentDidRender */
  async componentDidRender() {
    await this.goToPageTaskQueue.runTask();
  }
  /**
   * watch current change
   */
  async watchCurrentChange() {
    var _a;
    this.updateCurrent(this.current);
    this.currentStepUpdateEventEmitter.emit((_a = this.currentSteps[this.current]) === null || _a === void 0 ? void 0 : _a.stepKey);
    await this.renderer.gotoCarouselPage();
  }
  /** watch steps change */
  watchSteps() {
    this.updateCurrent(this.current);
  }
  /** watch nextStep method call*/
  nextStep() {
    this.updateCurrent(++this.current);
  }
  /** watch prevStep method call*/
  prevStep() {
    // In no-current-step mode, prevStep should keep -1 as the lower bound.
    const minIndex = this.allowNoCurrentStep && this.current <= -1 ? -1 : 0;
    const maxIndex = Math.max(0, this.currentSteps.length - 1);
    this.current = normalizeNumberByRanger(--this.current, minIndex, maxIndex);
  }
  /**
   * watch setCurrent method call
   *
   * @param {number} step - step index to set current
   */
  setCurrent(step) {
    this.updateCurrent(step);
  }
  /**
   * While direction changed from others direction to 'horizontal-compact',
   *   we need to goto the current step page in carousel.
   */
  async watchDirection() {
    await this.goToPageTaskQueue.enqueue({
      /* eslint-disable jsdoc/require-jsdoc */
      run: async () => {
        return await this.renderer.gotoCarouselPage();
      },
    }, false);
  }
  /**
   * Initialize step click observer
   */
  initStepClickObserver() {
    this.stepClickObserver = new Observer(this.canAcceptStepClickEvent.bind(this), this.updateCurrent.bind(this));
    changeManager.register(domNodeSubjectFactory.getSubject(this.host, VegaInternalProgressTrackerStepClick), this.stepClickObserver);
  }
  /**
   * Remove step click observer
   */
  removeStepClickObserver() {
    changeManager.unregister(domNodeSubjectFactory.getSubject(this.host, VegaInternalProgressTrackerStepClick), this.stepClickObserver);
  }
  /**
   * Check if can accept step click event
   *
   * @returns {boolean} - true if can accept, false otherwise.
   */
  canAcceptStepClickEvent() {
    return true;
  }
}
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentStepController.prototype, "currentSteps", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaProgressTrackerCurrentStepController.prototype, "current", void 0);
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentStepController.prototype, "allowNoCurrentStep", void 0);
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentStepController.prototype, "currentStepUpdateEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentStepController.prototype, "renderer", void 0);
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentStepController.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaProgressTrackerCurrentStepController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaProgressTrackerCurrentStepController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaProgressTrackerCurrentStepController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaProgressTrackerCurrentStepController.prototype, "componentDidRender", null);
__decorate([
  MapToComponentMethod('watchCurrentChange')
], VegaProgressTrackerCurrentStepController.prototype, "watchCurrentChange", null);
__decorate([
  MapToComponentMethod('watchSteps')
], VegaProgressTrackerCurrentStepController.prototype, "watchSteps", null);
__decorate([
  MapToComponentMethod('nextStep')
], VegaProgressTrackerCurrentStepController.prototype, "nextStep", null);
__decorate([
  MapToComponentMethod('prevStep')
], VegaProgressTrackerCurrentStepController.prototype, "prevStep", null);
__decorate([
  MapToComponentMethod('setCurrent')
], VegaProgressTrackerCurrentStepController.prototype, "setCurrent", null);
__decorate([
  MapToComponentMethod('watchDirection')
], VegaProgressTrackerCurrentStepController.prototype, "watchDirection", null);
