import { readFileSync, writeFileSync, existsSync } from "fs";
import { join } from "path";
import { logger } from "../../core/monitoring/logger.js";
import { ProjectIsolationManager } from "../../core/projects/project-isolation.js";
class LinearConfigManager {
  configPath;
  projectRoot;
  isolationManager;
  constructor(projectRoot) {
    this.projectRoot = projectRoot;
    this.configPath = join(
      projectRoot,
      ".stackmemory",
      "linear-auto-sync.json"
    );
    this.isolationManager = ProjectIsolationManager.getInstance();
  }
  /**
   * Load configuration from file
   */
  loadConfig() {
    if (!existsSync(this.configPath)) {
      return null;
    }
    try {
      const configData = readFileSync(this.configPath, "utf8");
      return JSON.parse(configData);
    } catch (error) {
      logger.error(
        "Failed to load Linear auto-sync configuration:",
        error
      );
      return null;
    }
  }
  /**
   * Save configuration to file
   */
  saveConfig(config) {
    const existingConfig = this.loadConfig() || this.getDefaultConfig();
    const updatedConfig = {
      ...existingConfig,
      ...config,
      lastUpdated: Date.now()
    };
    try {
      writeFileSync(this.configPath, JSON.stringify(updatedConfig, null, 2));
      logger.info("Linear auto-sync configuration saved");
    } catch (error) {
      logger.error(
        "Failed to save Linear auto-sync configuration:",
        error
      );
      throw error;
    }
  }
  /**
   * Get default configuration with project isolation
   */
  getDefaultConfig() {
    const projectId = this.isolationManager.getProjectIdentification(this.projectRoot);
    return {
      enabled: true,
      interval: 5,
      // 5 minutes
      direction: "bidirectional",
      conflictResolution: "newest_wins",
      retryAttempts: 3,
      retryDelay: 3e4,
      // 30 seconds
      quietHours: {
        start: 22,
        // 10 PM
        end: 7
        // 7 AM
      },
      // Project isolation from stable identification
      teamId: projectId.linearTeamId,
      organization: projectId.linearOrganization,
      workspaceFilter: projectId.workspaceFilter,
      projectPrefix: projectId.projectPrefix,
      lastUpdated: Date.now()
    };
  }
  /**
   * Convert to AutoSyncConfig format
   */
  toAutoSyncConfig(config) {
    const persistedConfig = config || this.loadConfig() || this.getDefaultConfig();
    return {
      enabled: persistedConfig.enabled,
      direction: persistedConfig.direction,
      defaultTeamId: void 0,
      // Will be set by sync engine
      autoSync: true,
      conflictResolution: persistedConfig.conflictResolution,
      syncInterval: persistedConfig.interval,
      interval: persistedConfig.interval,
      retryAttempts: persistedConfig.retryAttempts,
      retryDelay: persistedConfig.retryDelay,
      quietHours: persistedConfig.quietHours
    };
  }
  /**
   * Update specific configuration values
   */
  updateConfig(updates) {
    this.saveConfig(updates);
  }
  /**
   * Check if configuration exists
   */
  hasConfig() {
    return existsSync(this.configPath);
  }
  /**
   * Reset to default configuration
   */
  resetConfig() {
    this.saveConfig(this.getDefaultConfig());
  }
}
export {
  LinearConfigManager
};
//# sourceMappingURL=config.js.map
