"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pager_api_exports = {};
__export(pager_api_exports, {
  getPagerAPI: () => getPagerAPI
});
module.exports = __toCommonJS(pager_api_exports);
const getPrevActionableMessageIndex = (aiMessages, startIndex) => {
  for (let i = startIndex - 1; i >= 0; i--) {
    if (aiMessages[i].type !== "text") {
      return i;
    }
  }
  for (let i = aiMessages.length - 1; i > startIndex; i--) {
    if (aiMessages[i].type !== "text") {
      return i;
    }
  }
  return startIndex;
};
const getNextActionableMessageIndex = (aiMessages, startIndex) => {
  for (let i = startIndex + 1; i < aiMessages.length; i++) {
    if (aiMessages[i].type !== "text") {
      return i;
    }
  }
  for (let i = 0; i < startIndex; i++) {
    if (aiMessages[i].type !== "text") {
      return i;
    }
  }
  return startIndex;
};
const getPagerAPI = (aiMessages, activeMessageIndex, setActiveMessageIndex, setAiMessages) => {
  const actionableMessages = aiMessages.filter((m) => m.type !== "text" && m.type !== "theme");
  const total = actionableMessages.length;
  const current = actionableMessages.findIndex((m) => m === aiMessages[activeMessageIndex]);
  if (!total || current === -1) {
    return {
      current: 0,
      total: 0,
      currentUndone: false,
      toggleUndo: () => {
      },
      onPrev: () => {
      },
      onNext: () => {
      }
    };
  }
  const onPrev = () => {
    const prevIndex = getPrevActionableMessageIndex(aiMessages, activeMessageIndex);
    setActiveMessageIndex(prevIndex);
    const prevMessage = aiMessages[prevIndex];
    prevMessage.aiRegion?.overlay?.scrollIntoView({
      behavior: "instant",
      block: "center"
    });
  };
  const onNext = () => {
    const nextIndex = getNextActionableMessageIndex(aiMessages, activeMessageIndex);
    setActiveMessageIndex(nextIndex);
    const nextMessage = aiMessages[nextIndex];
    nextMessage.aiRegion?.overlay?.scrollIntoView({
      behavior: "instant",
      block: "center"
    });
  };
  const currentMessage = aiMessages[activeMessageIndex];
  return {
    current,
    total,
    onPrev,
    onNext,
    currentUndone: currentMessage.viewBefore,
    toggleUndo: () => {
      setAiMessages(
        (prevMessages) => prevMessages.map((msg, index) => {
          if (index === activeMessageIndex && (msg.type === "replace" || msg.type === "insert")) {
            return {
              ...msg,
              viewBefore: !msg.viewBefore
            };
          }
          return msg;
        })
      );
    }
  };
};
//# sourceMappingURL=pager-api.js.map
