import { PI_WEB_CAPABILITIES, type PiWebCapability, type PiWebRuntimeComponent, type PiWebServiceComponent } from "./apiTypes.js";

export { PI_WEB_CAPABILITIES };
export type { PiWebCapability };

export const KNOWN_PI_WEB_CAPABILITIES = Object.values(PI_WEB_CAPABILITIES);
const knownPiWebCapabilities: ReadonlySet<string> = new Set(KNOWN_PI_WEB_CAPABILITIES);

export const WEB_RUNTIME_CAPABILITIES = [PI_WEB_CAPABILITIES.sessionsDeleteArchived] as const satisfies readonly PiWebCapability[];
export const SESSIOND_RUNTIME_CAPABILITIES = [PI_WEB_CAPABILITIES.sessionsDeleteArchived] as const satisfies readonly PiWebCapability[];

const EFFECTIVE_CAPABILITY_REQUIREMENTS = {
  [PI_WEB_CAPABILITIES.sessionsDeleteArchived]: ["web", "sessiond"],
} as const satisfies Record<PiWebCapability, readonly PiWebServiceComponent[]>;

export function isPiWebCapability(value: unknown): value is PiWebCapability {
  return typeof value === "string" && knownPiWebCapabilities.has(value);
}

export function supportsPiWebCapability(source: { capabilities?: readonly PiWebCapability[] } | undefined, capability: PiWebCapability): boolean {
  return source?.capabilities?.includes(capability) === true;
}

export function effectivePiWebCapabilities(components: Partial<Record<PiWebServiceComponent, Pick<PiWebRuntimeComponent, "available" | "capabilities">>>): PiWebCapability[] {
  return KNOWN_PI_WEB_CAPABILITIES.filter((capability) => {
    const requiredComponents = EFFECTIVE_CAPABILITY_REQUIREMENTS[capability];
    return requiredComponents.every((component) => {
      const runtime = components[component];
      return runtime?.available === true && supportsPiWebCapability(runtime, capability);
    });
  });
}
