/**
 * GENERATED by `imprint emit` — DO NOT EDIT BY HAND.
 *
 * Tool: get_flight_calendar_prices
 * Site: google-flights
 * Intent: Get a calendar grid of lowest round-trip prices by departure date for a route over a date window.
 *
 * To regenerate: imprint emit ~/.imprint/google-flights/get_flight_calendar_prices/workflow.json --force
 */

import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import {
  executeWorkflow,
  type CredentialStore,
} from 'imprint/runtime';
import type { ToolResult, Workflow } from 'imprint/types';

const WORKFLOW: Workflow = {
  "toolName": "get_flight_calendar_prices",
  "intent": {
    "description": "Get a calendar grid of lowest round-trip prices by departure date for a route over a date window.",
    "userSaid": "searched for a round trip flight; SJC to SAN"
  },
  "parameters": [
    {
      "name": "origin",
      "type": "string",
      "description": "Origin airport IATA code, e.g. SJC",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "destination",
      "type": "string",
      "description": "Destination airport IATA code, e.g. SAN",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "start_date",
      "type": "string",
      "description": "Start of departure-date window, YYYY-MM-DD",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "end_date",
      "type": "string",
      "description": "End of departure-date window, YYYY-MM-DD",
      "verified": false,
      "verifyNote": "annotated"
    }
  ],
  "requests": [
    {
      "method": "POST",
      "url": "https://www.google.com/_/FlightsFrontendUi/data/travel.frontend.flights.FlightsFrontendService/GetCalendarPicker?f.sid=${state.f_sid}&bl=${state.bl}&hl=en-US&soc-app=162&soc-platform=1&soc-device=1&_reqid=1508023&rt=c",
      "headers": {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "X-Same-Domain": "1",
        "x-goog-ext-259736195-jspb": "[\"en-US\",\"US\",\"USD\",2,null,[420],null,null,7,[]]",
        "Referer": "https://www.google.com/travel/flights",
        "Accept-Language": "en-US,en;q=0.9"
      },
      "body": "f.req=%5Bnull%2C%22%5Bnull%2C%5Bnull%2Cnull%2C1%2Cnull%2C%5B%5D%2C1%2C%5B1%2C0%2C0%2C0%5D%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5B%5B%5B%5B%5C%22${param.origin}%5C%22%2C0%5D%5D%5D%2C%5B%5B%5B%5C%22${param.destination}%5C%22%2C0%5D%5D%5D%2Cnull%2C0%5D%2C%5B%5B%5B%5B%5C%22${param.destination}%5C%22%2C0%5D%5D%5D%2C%5B%5B%5B%5C%22${param.origin}%5C%22%2C0%5D%5D%5D%2Cnull%2C0%5D%5D%2Cnull%2Cnull%2Cnull%2C1%5D%2C%5B%5C%22${param.start_date}%5C%22%2C%5C%22${param.end_date}%5C%22%5D%2Cnull%2C%5B7%2C7%5D%5D%22%5D&",
      "effect": "safe"
    }
  ],
  "site": "google-flights",
  "bootstrap": {
    "url": "https://www.google.com/travel/flights?hl=en-US&curr=USD",
    "waitUntil": "domcontentloaded",
    "captures": [
      {
        "name": "f_sid",
        "required": false,
        "capability": "browser_bootstrap",
        "source": "html_regex",
        "pattern": "\"FdrFJe\":\"([^\"]+)\"",
        "group": 1
      },
      {
        "name": "bl",
        "required": false,
        "capability": "browser_bootstrap",
        "source": "html_regex",
        "pattern": "\"cfb2h\":\"([^\"]+)\"",
        "group": 1
      }
    ]
  },
  "parserModule": "./parser.ts",
  "requestTransformModule": "./request-transform.ts",
  "liveVerified": true
};

export interface GetFlightCalendarPricesInput {
  /** Origin airport IATA code, e.g. SJC */
  origin: string;
  /** Destination airport IATA code, e.g. SAN */
  destination: string;
  /** Start of departure-date window, YYYY-MM-DD */
  start_date: string;
  /** End of departure-date window, YYYY-MM-DD */
  end_date: string;
}

export async function getFlightCalendarPrices(
  input: GetFlightCalendarPricesInput,
  opts: { credentials?: CredentialStore; fetchImpl?: typeof fetch; initialState?: Record<string, unknown> } = {},
): Promise<ToolResult> {
  const __dirname = dirname(fileURLToPath(import.meta.url));
  const params: Record<string, string | number | boolean> = {
    origin: input.origin,
    destination: input.destination,
    start_date: input.start_date,
    end_date: input.end_date,

  };
  return executeWorkflow({
    workflow: WORKFLOW,
    params,
    credentials: opts.credentials,
    fetchImpl: opts.fetchImpl,
    initialState: opts.initialState,
    workflowPath: join(__dirname, 'workflow.json'),
  });
}

export { WORKFLOW };
