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 { MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { SynchronizeTaskQueue } from '../queue/synchronize-task-queue';
import { LogUtility } from '../../utils/log';
/**
 * The current slimmer contains a task queue and will call the task after component re-render.
 */
export class AutoRunWhenReRenderTaskQueueSlimmer extends VegaSlimmer {
  constructor(maxCount) {
    super();
    this.autoRunTaskQueue = new SynchronizeTaskQueue(maxCount);
  }
  /**
   * Update the caret position after the component re-render
   */
  async executeTaskHook() {
    if (this.autoRunTaskQueue.queue.length) {
      await this.autoRunTaskQueue.runTask();
    }
  }
  /**
   * Push the update task to a queue.
   *
   * @param {boolean} [immediatelyRun] - Should the task run immediately. default is false
   */
  enqueueTheTaskIntoQueue(immediatelyRun = false) {
    void this.autoRunTaskQueue.enqueue({
      /** Add task to the queue and run it until component did render */
      run: async () => {
        try {
          await this.doTask();
        }
        catch (e) {
          LogUtility.error('The auto task run failed.', e);
        }
      },
    }, immediatelyRun);
  }
}
__decorate([
  MapToComponentMethod('componentDidUpdate')
], AutoRunWhenReRenderTaskQueueSlimmer.prototype, "executeTaskHook", null);
