import { defineStore } from 'pinia';

interface InputState {
  inputMessage: string;
  inputLineCount: number;
  inputIsMultiline: boolean;
  inputIsFocused: boolean;
  quickMenuOpen: boolean;
  toolMenuOpen: boolean;
  settingsOpen: boolean;
  imagePickerOpen: boolean;
  selectedImages: string[];
}

export const useInputStore = defineStore('input', {
  state: (): InputState => ({
    inputMessage: '',
    inputLineCount: 1,
    inputIsMultiline: false,
    inputIsFocused: false,
    quickMenuOpen: false,
    toolMenuOpen: false,
    settingsOpen: false,
    imagePickerOpen: false,
    selectedImages: []
  }),
  actions: {
    setInputMessage(value: string) {
      this.inputMessage = value;
    },
    clearInputMessage() {
      this.inputMessage = '';
    },
    setInputLineCount(count: number) {
      this.inputLineCount = count;
    },
    setInputMultiline(multiline: boolean) {
      this.inputIsMultiline = multiline;
    },
    setInputFocused(focused: boolean) {
      this.inputIsFocused = focused;
    },
    openQuickMenu() {
      this.quickMenuOpen = true;
    },
    setQuickMenuOpen(open: boolean) {
      this.quickMenuOpen = open;
      if (!open) {
        this.toolMenuOpen = false;
        this.settingsOpen = false;
      }
    },
    toggleQuickMenu() {
      const next = !this.quickMenuOpen;
      this.setQuickMenuOpen(next);
      return next;
    },
    closeMenus() {
      this.quickMenuOpen = false;
      this.toolMenuOpen = false;
      this.settingsOpen = false;
    },
    toggleToolMenu() {
      this.toolMenuOpen = !this.toolMenuOpen;
      return this.toolMenuOpen;
    },
    setToolMenuOpen(open: boolean) {
      this.toolMenuOpen = open;
    },
    toggleSettingsMenu() {
      this.settingsOpen = !this.settingsOpen;
      return this.settingsOpen;
    },
    setSettingsOpen(open: boolean) {
      this.settingsOpen = open;
    },
    setImagePickerOpen(open: boolean) {
      this.imagePickerOpen = open;
    },
    setSelectedImages(list: string[]) {
      this.selectedImages = list.slice(0, 9);
    },
    addSelectedImage(path: string) {
      if (!path) return;
      const next = Array.from(new Set([...this.selectedImages, path]));
      this.selectedImages = next.slice(0, 9);
    },
    removeSelectedImage(path: string) {
      this.selectedImages = this.selectedImages.filter(item => item !== path);
    },
    clearSelectedImages() {
      this.selectedImages = [];
    }
  }
});
