import {
  BotActionStatus,
  type BotActionResult,
} from "./generated/soulfire/bot_live_pb.js";

export class SoulFireActionError extends Error {
  public constructor(public readonly result: BotActionResult) {
    super(result.error ?? `Bot action ${result.actionId} did not complete`);
    this.name = "SoulFireActionError";
  }
}

export function requireCompletedAction(
  result: BotActionResult | undefined,
): BotActionResult {
  if (result === undefined) {
    throw new Error("SoulFire did not return a bot action result");
  }
  if (result.status !== BotActionStatus.COMPLETED) {
    throw new SoulFireActionError(result);
  }
  return result;
}
