// src/testYourself/server/ConfigService.ts
var LocalStorageAdapter = class {
  constructor() {
    this.STORAGE_KEY = "test-yourself-configs";
    this.DEFAULT_KEY = "test-yourself-default-config";
  }
  async getAllConfigsData() {
    if (typeof window === "undefined") return [];
    try {
      const data = localStorage.getItem(this.STORAGE_KEY);
      return data ? JSON.parse(data) : [];
    } catch (error) {
      console.error("\u8BFB\u53D6\u914D\u7F6E\u5931\u8D25:", error);
      return [];
    }
  }
  async saveAllConfigsData(configs) {
    if (typeof window === "undefined") return;
    try {
      localStorage.setItem(this.STORAGE_KEY, JSON.stringify(configs));
    } catch (error) {
      console.error("\u4FDD\u5B58\u914D\u7F6E\u5931\u8D25:", error);
      throw error;
    }
  }
  async saveConfig(config) {
    const configs = await this.getAllConfigsData();
    const existingIndex = configs.findIndex((c) => c.id === config.id);
    if (existingIndex >= 0) {
      configs[existingIndex] = config;
    } else {
      configs.push(config);
    }
    await this.saveAllConfigsData(configs);
  }
  async getConfig(id) {
    const configs = await this.getAllConfigsData();
    return configs.find((c) => c.id === id) || null;
  }
  async getAllConfigs() {
    return this.getAllConfigsData();
  }
  async deleteConfig(id) {
    const configs = await this.getAllConfigsData();
    const filtered = configs.filter((c) => c.id !== id);
    await this.saveAllConfigsData(filtered);
  }
  async updateConfig(id, config) {
    const configs = await this.getAllConfigsData();
    const index = configs.findIndex((c) => c.id === id);
    if (index >= 0) {
      configs[index] = { ...config, updatedAt: Date.now() };
      await this.saveAllConfigsData(configs);
    } else {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
  }
  async setDefaultConfig(id) {
    if (typeof window === "undefined") return;
    const config = await this.getConfig(id);
    if (!config) {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
    const configs = await this.getAllConfigsData();
    configs.forEach((c) => c.isDefault = c.id === id);
    await this.saveAllConfigsData(configs);
    localStorage.setItem(this.DEFAULT_KEY, id);
  }
  async getDefaultConfig() {
    if (typeof window === "undefined") return null;
    try {
      const defaultId = localStorage.getItem(this.DEFAULT_KEY);
      if (defaultId) {
        return await this.getConfig(defaultId);
      }
      const configs = await this.getAllConfigsData();
      return configs.find((c) => c.isDefault) || null;
    } catch (error) {
      console.error("\u83B7\u53D6\u9ED8\u8BA4\u914D\u7F6E\u5931\u8D25:", error);
      return null;
    }
  }
};
var MemoryStorageAdapter = class {
  constructor() {
    this.configs = /* @__PURE__ */ new Map();
    this.defaultConfigId = null;
  }
  async saveConfig(config) {
    this.configs.set(config.id, config);
  }
  async getConfig(id) {
    return this.configs.get(id) || null;
  }
  async getAllConfigs() {
    return Array.from(this.configs.values());
  }
  async deleteConfig(id) {
    this.configs.delete(id);
    if (this.defaultConfigId === id) {
      this.defaultConfigId = null;
    }
  }
  async updateConfig(id, config) {
    if (!this.configs.has(id)) {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
    this.configs.set(id, { ...config, updatedAt: Date.now() });
  }
  async setDefaultConfig(id) {
    if (!this.configs.has(id)) {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
    this.configs.forEach((c) => c.isDefault = c.id === id);
    this.defaultConfigId = id;
  }
  async getDefaultConfig() {
    if (this.defaultConfigId) {
      return this.configs.get(this.defaultConfigId) || null;
    }
    for (const config of this.configs.values()) {
      if (config.isDefault) {
        return config;
      }
    }
    return null;
  }
};
var ConfigService = class {
  constructor(options = {}) {
    this.cache = /* @__PURE__ */ new Map();
    const {
      storageType = "localStorage",
      customStorage,
      enableCache = true
    } = options;
    this.enableCache = enableCache;
    if (customStorage) {
      this.storage = customStorage;
    } else if (storageType === "memory") {
      this.storage = new MemoryStorageAdapter();
    } else {
      this.storage = new LocalStorageAdapter();
    }
  }
  /**
   * 初始化服务（如果需要）
   */
  async init() {
  }
  /**
   * 生成唯一ID
   */
  generateId() {
    return "config_" + Date.now() + "_" + Math.random().toString(36).substring(2, 9);
  }
  /**
   * 保存配置
   */
  async saveConfig(config) {
    await this.storage.saveConfig(config);
    if (config.isDefault) {
      await this.storage.setDefaultConfig(config.id);
    }
    if (this.enableCache) {
      this.cache.set(config.id, config);
    }
  }
  /**
   * 创建新配置
   */
  async createConfig(name, config, description, isDefault = false) {
    const savedConfig = {
      id: this.generateId(),
      name,
      description,
      config,
      createdAt: Date.now(),
      updatedAt: Date.now(),
      isDefault
    };
    await this.storage.saveConfig(savedConfig);
    if (isDefault) {
      await this.storage.setDefaultConfig(savedConfig.id);
    }
    if (this.enableCache) {
      this.cache.set(savedConfig.id, savedConfig);
    }
    return savedConfig;
  }
  /**
   * 获取配置
   */
  async getConfig(id) {
    if (this.enableCache && this.cache.has(id)) {
      return this.cache.get(id);
    }
    const config = await this.storage.getConfig(id);
    if (config && this.enableCache) {
      this.cache.set(id, config);
    }
    return config;
  }
  /**
   * 获取所有配置列表
   */
  async getAllConfigs() {
    return await this.storage.getAllConfigs();
  }
  /**
   * 获取配置列表（精简版）
   */
  async getConfigList() {
    const configs = await this.getAllConfigs();
    return configs.map((c) => ({
      id: c.id,
      name: c.name,
      description: c.description,
      resultCount: c.config.results.length,
      createdAt: c.createdAt,
      updatedAt: c.updatedAt,
      isDefault: c.isDefault
    }));
  }
  /**
   * 更新配置
   */
  async updateConfig(id, updates) {
    const existing = await this.getConfig(id);
    if (!existing) {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
    const updated = {
      ...existing,
      ...updates,
      updatedAt: Date.now()
    };
    await this.storage.updateConfig(id, updated);
    if (updates.isDefault) {
      await this.storage.setDefaultConfig(id);
    }
    if (this.enableCache) {
      this.cache.set(id, updated);
    }
    return updated;
  }
  /**
   * 删除配置
   */
  async deleteConfig(id) {
    await this.storage.deleteConfig(id);
    if (this.enableCache) {
      this.cache.delete(id);
    }
  }
  /**
   * 设置默认配置
   */
  async setDefaultConfig(id) {
    await this.storage.setDefaultConfig(id);
    if (this.enableCache) {
      this.cache.clear();
    }
  }
  /**
   * 获取默认配置
   */
  async getDefaultConfig() {
    return await this.storage.getDefaultConfig();
  }
  /**
   * 清空缓存
   */
  clearCache() {
    this.cache.clear();
  }
  /**
   * 导出配置（JSON格式）
   */
  async exportConfig(id) {
    const config = await this.getConfig(id);
    if (!config) {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
    return JSON.stringify(config, null, 2);
  }
  /**
   * 导入配置
   */
  async importConfig(jsonString) {
    try {
      const data = JSON.parse(jsonString);
      const newConfig = {
        ...data,
        id: this.generateId(),
        createdAt: Date.now(),
        updatedAt: Date.now(),
        isDefault: false
        // 导入的配置不设为默认
      };
      await this.storage.saveConfig(newConfig);
      if (this.enableCache) {
        this.cache.set(newConfig.id, newConfig);
      }
      return newConfig;
    } catch (error) {
      throw new Error("\u5BFC\u5165\u914D\u7F6E\u5931\u8D25: " + error);
    }
  }
  /**
   * 复制配置
   */
  async duplicateConfig(id, newName) {
    const original = await this.getConfig(id);
    if (!original) {
      throw new Error("\u914D\u7F6E\u4E0D\u5B58\u5728: " + id);
    }
    const duplicated = {
      ...original,
      id: this.generateId(),
      name: newName || original.name + " (\u526F\u672C)",
      createdAt: Date.now(),
      updatedAt: Date.now(),
      isDefault: false
    };
    await this.storage.saveConfig(duplicated);
    if (this.enableCache) {
      this.cache.set(duplicated.id, duplicated);
    }
    return duplicated;
  }
  /**
   * 批量删除配置
   */
  async deleteConfigs(ids) {
    for (const id of ids) {
      await this.deleteConfig(id);
    }
  }
};
function createConfigService(options) {
  return new ConfigService(options);
}
var defaultServiceInstance = null;
function getDefaultConfigService() {
  if (!defaultServiceInstance) {
    defaultServiceInstance = createConfigService();
  }
  return defaultServiceInstance;
}

export { ConfigService, createConfigService, getDefaultConfigService };
//# sourceMappingURL=chunk-HDEOCX2L.mjs.map
//# sourceMappingURL=chunk-HDEOCX2L.mjs.map