import type { CommandContext, QueryContext } from "./types";
import type { Result } from "./result";
import { InsufficientPermissionError } from "./errors";
import { ok, err } from "./result";

export function requirePermission(
  ctx: CommandContext | QueryContext,
  key: string,
): Result<void, InstanceType<typeof InsufficientPermissionError>> {
  if (!ctx.permissions?.includes(key)) {
    return err(new InsufficientPermissionError(ctx.actorId, key));
  }
  return ok(undefined);
}
