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 { InjectVegaSlimmer, MapToComponentField, MapToComponentMethod, methodPlaceholder, } from 'vega-slimmer/core';
import { PageResizeObserverSlimmer } from '../../../../helpers/slimmers/page-resize-observer-slimmer';
import responsiveFormatFacade from '../../../../helpers/formatter/responsive-map-formatter/responsive-format-facade';
import { isResponsiveType } from '../../../../types/type-guard';
import { getCurrentBreakpoint } from '../../../../utils/breakpoints';
import { AutoRunWhenReRenderTaskQueueSlimmer } from '../../../../helpers/slimmers/auto-run-when-re-render-task-queue-slimmer.abstract';
/**
 * Controls the current direction of the progress tracker component.
 */
export class VegaProgressTrackerCurrentDirectionController extends AutoRunWhenReRenderTaskQueueSlimmer {
  constructor() {
    super(...arguments);
    this.pageResizeObserverSlimmer = new PageResizeObserverSlimmer({
      /*
       * The below method is e2e-test covered in
       * @see{module:vega-progress-tracker-resize-e2e}
       */
      // eslint-disable-next-line jsdoc/require-jsdoc
      callback: /* istanbul ignore next */ async () => {
        const currentAtResize = this.current;
        const directionBeforeResize = this.currentDirection;
        this.setCurrentDirection();
        if (directionBeforeResize === this.currentDirection) {
          await this.renderer.gotoCarouselPage(currentAtResize);
          return;
        }
        // Sync carousel page after component re-render when direction changes.
        this.enqueueSyncCarouselPage(currentAtResize);
      },
    });
  }
  /** connectedCallback */
  connectedCallback() {
    methodPlaceholder();
  }
  /** disconnectedCallback */
  disconnectedCallback() {
    methodPlaceholder();
  }
  /** componentWillLoad */
  componentWillLoad() {
    this.setCurrentDirection();
  }
  /** componentDidLoad */
  componentDidLoad() {
    methodPlaceholder();
  }
  /** watchDirection */
  watchDirection() {
    this.setCurrentDirection();
  }
  /**
   * No-op: carousel sync is handled via closure-captured value in enqueueSyncCarouselPage().
   */
  async doTask() {
    methodPlaceholder();
  }
  /**
   * Enqueue a carousel page sync with the value captured in a closure,
   * avoiding shared mutable state between enqueue and execution.
   *
   * @param {number} page - The carousel page index to sync to
   */
  enqueueSyncCarouselPage(page) {
    void this.autoRunTaskQueue.enqueue({
      /** Sync carousel to the captured page after re-render */
      run: async () => {
        await this.renderer.gotoCarouselPage(page);
      },
    }, false);
  }
  /**
   * The function sets the current direction of a progress tracker
   * based on the component's direction and the current breakpoint.
   */
  setCurrentDirection() {
    let direction = responsiveFormatFacade.formatCustom(this.direction) || 'vertical';
    if (['default', 'S'].includes(getCurrentBreakpoint()) && !isResponsiveType(this.direction)) {
      direction = 'horizontal-compact';
    }
    this.currentDirection = direction;
  }
}
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentDirectionController.prototype, "direction", void 0);
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentDirectionController.prototype, "current", void 0);
__decorate([
  MapToComponentField()
], VegaProgressTrackerCurrentDirectionController.prototype, "renderer", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaProgressTrackerCurrentDirectionController.prototype, "currentDirection", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaProgressTrackerCurrentDirectionController.prototype, "pageResizeObserverSlimmer", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaProgressTrackerCurrentDirectionController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaProgressTrackerCurrentDirectionController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaProgressTrackerCurrentDirectionController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaProgressTrackerCurrentDirectionController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('watchDirection')
], VegaProgressTrackerCurrentDirectionController.prototype, "watchDirection", null);
