import { Container } from "../container/container"
import { ReplayEncoder } from "../utils/replay-encoder"

export class LinkFormatter {
  container: Container
  replayUrl: string = ""
  constructor(container: Container) {
    this.container = container
  }

  getReplayUri(state: any): string {
    const serialised = typeof state === "string" ? state : JSON.stringify(state)
    const compressed = ReplayEncoder.crush(serialised)
    const currentPage =
      globalThis.location?.href?.split("?")[0] ?? "http://localhost/"
    const prefix =
      this.replayUrl ||
      `${currentPage}?ruletype=${this.container.rules.rulename}&state=`
    return `${prefix}${ReplayEncoder.fullyEncodeURI(compressed)}`
  }

  getHiScoreUri(state: any, score: number): string {
    state.score = score
    return this.getReplayUri(state)
  }

  wholeGameLink(game: any) {
    this.container.ballTray.addGame(game)
  }
}
