/**
 * Feature flags domain. Deployment and activation are separate operations:
 * flags can change at runtime without redeploy. Unknown flags are OFF
 * (fail-safe default for risky paths).
 */

export interface FeatureFlagProvider {
  isEnabled(name: string): boolean;
}

export class InMemoryFeatureFlagStore implements FeatureFlagProvider {
  private readonly state: Map<string, boolean>;

  constructor(initial: Readonly<Record<string, boolean>> = {}) {
    this.state = new Map(
      Object.entries(initial).map(([name, enabled]) => [name.toLowerCase(), enabled]),
    );
  }

  isEnabled(name: string): boolean {
    return this.state.get(name.toLowerCase()) === true;
  }

  /** Runtime mutation: activation does not require a redeploy. */
  setEnabled(name: string, enabled: boolean): void {
    this.state.set(name.toLowerCase(), enabled);
  }
}

export function createFlagStore(
  initial: Readonly<Record<string, boolean>> = {},
): InMemoryFeatureFlagStore {
  return new InMemoryFeatureFlagStore(initial);
}
