import type { Page } from "playwright";
import type { AxelSelectionBridge } from "@/workers/rpa/axel-selection-bridge";
import { TRIGGER_GOOGLE_PLACE_CHANGED } from "@/workers/rpa/fix-place/browser-eval-scripts";

const PLACE_COMMIT_TIMEOUT_MS = 8_000;

export async function commitViaGooglePlaceEvent(
  page: Page,
  bridge: AxelSelectionBridge,
  placeId: string,
  description: string,
): Promise<{ ok: boolean }> {
  const result = (await page.evaluate(TRIGGER_GOOGLE_PLACE_CHANGED, {
    pid: placeId,
    desc: description,
  })) as { ok?: boolean } | null;

  if (!result?.ok) {
    return { ok: false };
  }

  const committed = await bridge.waitForPlaceCommit(
    placeId,
    PLACE_COMMIT_TIMEOUT_MS,
  );
  return { ok: committed };
}
