"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../_vendor/@legendapp/state/index.cjs");
const sync = require("../_vendor/@legendapp/state/sync.cjs");
const localStorage = require("../_vendor/@legendapp/state/persist-plugins-local-storage.cjs");
const INITIAL_STATE = {
  history: [],
  currentIndex: -1,
  maxHistorySize: 100,
  tempInput: null
};
const commandHistoryStore$ = index.observable(INITIAL_STATE);
sync.syncObservable(commandHistoryStore$, {
  persist: {
    name: "prompt-kit-command-history",
    plugin: localStorage.ObservablePersistLocalStorage
  }
});
const commandHistoryActions = {
  // 添加命令到历史记录
  addCommand: (command) => {
    const trimmedCommand = command.trim();
    if (!trimmedCommand) return;
    const currentHistory = commandHistoryStore$.history.get();
    if (currentHistory[0] === trimmedCommand) return;
    const newHistory = [trimmedCommand, ...currentHistory];
    const maxSize = commandHistoryStore$.maxHistorySize.get();
    if (newHistory.length > maxSize) {
      newHistory.splice(maxSize);
    }
    commandHistoryStore$.history.set(newHistory);
    commandHistoryStore$.currentIndex.set(-1);
    commandHistoryStore$.tempInput.set(null);
  },
  // 向上导航（查看更旧的命令）
  navigateUp: (currentInput) => {
    const history = commandHistoryStore$.history.get();
    const currentIndex = commandHistoryStore$.currentIndex.get();
    if (history.length === 0) return null;
    if (currentIndex === -1) {
      commandHistoryStore$.tempInput.set(currentInput);
    }
    if (currentIndex < history.length - 1) {
      const newIndex = currentIndex + 1;
      commandHistoryStore$.currentIndex.set(newIndex);
      return history[newIndex];
    }
    return null;
  },
  // 向下导航（查看更新的命令）
  navigateDown: () => {
    const history = commandHistoryStore$.history.get();
    const currentIndex = commandHistoryStore$.currentIndex.get();
    const tempInput = commandHistoryStore$.tempInput.get();
    if (currentIndex === -1) {
      return null;
    }
    const newIndex = currentIndex - 1;
    commandHistoryStore$.currentIndex.set(newIndex);
    if (newIndex === -1) {
      const result = tempInput;
      commandHistoryStore$.tempInput.set(null);
      return result !== null ? result : "";
    }
    return history[newIndex];
  },
  // 重置导航状态
  resetNavigation: () => {
    commandHistoryStore$.currentIndex.set(-1);
    commandHistoryStore$.tempInput.set(null);
  },
  // 清空历史记录
  clearHistory: () => {
    commandHistoryStore$.history.set([]);
    commandHistoryStore$.currentIndex.set(-1);
    commandHistoryStore$.tempInput.set(null);
  },
  // 设置最大历史记录数
  setMaxHistorySize: (size) => {
    commandHistoryStore$.maxHistorySize.set(size);
    const currentHistory = commandHistoryStore$.history.get();
    if (currentHistory.length > size) {
      commandHistoryStore$.history.set(currentHistory.slice(0, size));
    }
  }
};
exports.commandHistoryActions = commandHistoryActions;
exports.commandHistoryStore$ = commandHistoryStore$;
//# sourceMappingURL=command-history-store.cjs.map
