/**
 * @file localStorage.ts - Functions to retrieve from, update, and delete from localStorage.
 * Used for user preference management.
 */

import {
  LOCAL_STORAGE_AUTO_OPEN_MODE,
  LOCAL_STORAGE_AUTO_SAVE_MODE,
  LOCAL_STORAGE_QUEUE_SHOW_FOLDER_FIELDS,
  SESSION_STORAGE_SELECTED_PROCESS,
  SESSION_STORAGE_SELECTED_QUEUE,
} from '../constants.ts';
import type { TablePageSizeType } from '../components/DataTable.tsx';

/**
 * Returns the process ID stored in the browser's sessionStorage.
 *
 * @returns The stored process ID or null if none found
 */
export function getStoredProcessId(): number | null {
  return Number(sessionStorage.getItem(SESSION_STORAGE_SELECTED_PROCESS));
}

/**
 * Updates the process ID stored in the browser's sessionStorage.
 *
 * @param processId The selected process ID
 */
export function setStoredProcessId(processId: number): void {
  sessionStorage.setItem(
    SESSION_STORAGE_SELECTED_PROCESS,
    processId.toString()
  );
}

/**
 * Clears the process ID stored in the browser's sessionStorage.
 */
export function clearStoredProcessId(): void {
  sessionStorage.removeItem(SESSION_STORAGE_SELECTED_PROCESS);
}

/**
 * Returns the queue ID stored in the browser's sessionStorage.
 *
 * @returns The stored queue ID or null if none found
 */
export function getStoredQueueId(): number | null {
  return Number(sessionStorage.getItem(SESSION_STORAGE_SELECTED_QUEUE));
}

/**
 * Updates the queue ID stored in the browser's sessionStorage.
 *
 * @param queueId The selected queue ID
 */
export function setStoredQueueId(queueId: number): void {
  sessionStorage.setItem(SESSION_STORAGE_SELECTED_QUEUE, queueId.toString());
}

/**
 * Clears the queue ID stored in the browser's sessionStorage.
 */
export function clearStoredQueueId(): void {
  sessionStorage.removeItem(SESSION_STORAGE_SELECTED_QUEUE);
}

/**
 * Returns the preference for whether a ContentCard (identified by its localStorage key) should be expanded.
 *
 * @param localStorageKey localStorage key name for card
 * @returns The stored preference (true/false) or null
 */
export function getStoredCardExpanded(localStorageKey: string): boolean | null {
  const value = localStorage.getItem(localStorageKey);

  if (value === null) {
    return null;
  } else {
    return value === 'true';
  }
}

/**
 * Updates the preference for whether a ContendCard (identified by its localStorage key) should be expanded.
 *
 * @param localStorageKey localStorage key name for card
 * @param expanded The selected preference (true/false)
 */
export function setStoredCardExpanded(
  localStorageKey: string,
  expanded: boolean
): void {
  localStorage.setItem(localStorageKey, expanded.toString());
}

/**
 * Clears the preference for whether a ContentCard (identified by its localStorage key) should be expanded.
 *
 * @param localStorageKey localStorage key name for card
 */
export function clearStoredCardExpanded(localStorageKey: string): void {
  localStorage.removeItem(localStorageKey);
}

/**
 * Retrieves the stored pagination page size for a given DataTable (identified by its localStorage key) from localStorage.
 *
 * @param localStorageKey localStorage key name for table
 * @returns The stored page size or null
 */
export function getStoredTablePagination(
  localStorageKey: string
): TablePageSizeType | null {
  return Number(localStorage.getItem(localStorageKey)) as TablePageSizeType;
}

/**
 * Updates the stored pagination page size for a given DataTable (identified by its localStorage key) from localStorage.
 *
 * @param localStorageKey localStorage key name for table
 * @param pageSize The selected page size
 */
export function setStoredTablePagination(
  localStorageKey: string,
  pageSize: TablePageSizeType
): void {
  localStorage.setItem(localStorageKey, pageSize.toString());
}

/**
 * Clears the stored pagination page size for a given DataTable (identified by its localStorage key) from localStorage.
 *
 * @param localStorageKey localStorage key name for table
 */
export function clearStoredTablePagination(localStorageKey: string): void {
  localStorage.removeItem(localStorageKey);
}

/**
 * Returns the preference for showing folder fields in the queue-based work item selection table stored in the browser's localStorage.
 *
 * @returns The stored preference (true/false) or null
 */
export function getStoredQueueShowFolderFields(): boolean | null {
  const value = localStorage.getItem(LOCAL_STORAGE_QUEUE_SHOW_FOLDER_FIELDS);

  if (value === null) {
    return null;
  } else {
    return value === 'true';
  }
}

/**
 * Updates the preference for showing folder fields in the queue-based work item selection table stored in the browser's localStorage.
 *
 * @param preference The selected preference (true/false)
 */
export function setStoredQueueShowFolderFields(preference: boolean): void {
  localStorage.setItem(
    LOCAL_STORAGE_QUEUE_SHOW_FOLDER_FIELDS,
    preference.toString()
  );
}

/**
 * Clears the preference for showing folder fields in the queue-based work item selection table stored in the browser's localStorage.
 */
export function clearStoredQueueShowFolderFields(): void {
  localStorage.removeItem(LOCAL_STORAGE_QUEUE_SHOW_FOLDER_FIELDS);
}

export type SaveMode = 'manual' | 'auto';

/**
 * Returns the save mode setting stored in the browser's localStorage.
 *
 * @returns The stored save mode setting
 */
export function getStoredSaveMode(): SaveMode | null {
  return localStorage.getItem(LOCAL_STORAGE_AUTO_SAVE_MODE) as SaveMode;
}

/**
 * Updates the save mode stored in the browser's localStorage.
 *
 * @param saveMode The selected save mode
 */
export function setStoredSaveMode(saveMode: SaveMode): void {
  localStorage.setItem(LOCAL_STORAGE_AUTO_SAVE_MODE, saveMode);
}

/**
 * Clears the save mode stored in the browser's localStorage.
 */
export function clearStoredSaveMode(): void {
  localStorage.removeItem(LOCAL_STORAGE_AUTO_SAVE_MODE);
}

export type OpenMode = 'manual' | 'inbox' | 'queue';

/**
 * Returns the open mode setting stored in the browser's localStorage.
 *
 * @returns The stored open mode setting
 */
export function getStoredOpenMode(): OpenMode | null {
  return localStorage.getItem(LOCAL_STORAGE_AUTO_OPEN_MODE) as OpenMode;
}

/**
 * Updates the open mode stored in the browser's localStorage.
 *
 * @param openMode The selected open mode
 */
export function setStoredOpenMode(openMode: OpenMode): void {
  localStorage.setItem(LOCAL_STORAGE_AUTO_OPEN_MODE, openMode);
}

/**
 * Clears the open mode stored in the browser's localStorage.
 */
export function clearStoredOpenMode(): void {
  localStorage.removeItem(LOCAL_STORAGE_AUTO_OPEN_MODE);
}
