/**
 * 本地存储工具函数
 * 用于管理应用程序的本地存储数据
 */

// 存储键名常量
const STORAGE_KEYS = {
  COZE_API_TOKEN: 'coze_api_token',
  COZE_WORKFLOW_IDS: 'coze_workflow_ids',
  USER_SETTINGS: 'user_settings',
};

/**
 * 保存Coze API Token到本地存储
 * @param {string} token API Token
 */
export const saveCozeApiToken = (token) => {
  try {
    localStorage.setItem(STORAGE_KEYS.COZE_API_TOKEN, token);
    return true;
  } catch (error) {
    console.error('保存Coze API Token失败:', error);
    return false;
  }
};

/**
 * 从本地存储获取Coze API Token
 * @returns {string|null} 存储的API Token，如果不存在则返回null
 */
export const getCozeApiToken = () => {
  try {
    return localStorage.getItem(STORAGE_KEYS.COZE_API_TOKEN);
  } catch (error) {
    console.error('获取Coze API Token失败:', error);
    return null;
  }
};

/**
 * 删除存储的Coze API Token
 * @returns {boolean} 操作是否成功
 */
export const removeCozeApiToken = () => {
  try {
    localStorage.removeItem(STORAGE_KEYS.COZE_API_TOKEN);
    return true;
  } catch (error) {
    console.error('删除Coze API Token失败:', error);
    return false;
  }
};

/**
 * 保存工作流ID到本地存储
 * @param {Object} workflowIds 工作流ID映射对象
 */
export const saveWorkflowIds = (workflowIds) => {
  try {
    localStorage.setItem(STORAGE_KEYS.COZE_WORKFLOW_IDS, JSON.stringify(workflowIds));
    return true;
  } catch (error) {
    console.error('保存工作流ID失败:', error);
    return false;
  }
};

/**
 * 从本地存储获取工作流ID
 * @returns {Object|null} 存储的工作流ID映射对象，如果不存在则返回null
 */
export const getWorkflowIds = () => {
  try {
    const workflowIds = localStorage.getItem(STORAGE_KEYS.COZE_WORKFLOW_IDS);
    return workflowIds ? JSON.parse(workflowIds) : null;
  } catch (error) {
    console.error('获取工作流ID失败:', error);
    return null;
  }
};

/**
 * 保存用户设置到本地存储
 * @param {Object} settings 用户设置对象
 */
export const saveUserSettings = (settings) => {
  try {
    // 获取现有设置
    const existingSettings = getUserSettings() || {};
    // 合并设置
    const newSettings = { ...existingSettings, ...settings };
    localStorage.setItem(STORAGE_KEYS.USER_SETTINGS, JSON.stringify(newSettings));
    return true;
  } catch (error) {
    console.error('保存用户设置失败:', error);
    return false;
  }
};

/**
 * 从本地存储获取用户设置
 * @returns {Object|null} 存储的用户设置对象，如果不存在则返回null
 */
export const getUserSettings = () => {
  try {
    const settings = localStorage.getItem(STORAGE_KEYS.USER_SETTINGS);
    return settings ? JSON.parse(settings) : null;
  } catch (error) {
    console.error('获取用户设置失败:', error);
    return null;
  }
};

/**
 * 检查浏览器是否支持本地存储
 * @returns {boolean} 是否支持本地存储
 */
export const isLocalStorageSupported = () => {
  try {
    const testKey = '___test___';
    localStorage.setItem(testKey, testKey);
    const result = localStorage.getItem(testKey) === testKey;
    localStorage.removeItem(testKey);
    return result;
  } catch (e) {
    return false;
  }
};

export default {
  saveCozeApiToken,
  getCozeApiToken,
  removeCozeApiToken,
  saveWorkflowIds,
  getWorkflowIds,
  saveUserSettings,
  getUserSettings,
  isLocalStorageSupported,
}; 