import { readFileSync } from "node:fs"
import { join } from "node:path"

import { describe, expect, test } from "@jest/globals"
import { IndentationText, Project } from "ts-morph"

import { processSourceFile } from "./lokalise"

describe("Lokalise codemod", () => {
  test("serverComponent: intl.formatMessage with only id", () => {
    const input = readFileSync(
      join(__dirname, "./fixtures/serverComponentOnlyId.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const expected = readFileSync(
      join(__dirname, "./expectations/serverComponentOnlyId.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const project = new Project({
      manipulationSettings: {
        indentationText: IndentationText.TwoSpaces,
      },
    })
    const sourceFile = project.createSourceFile(
      "./fixtures/serverComponent.tsx",
      input
    )

    processSourceFile(sourceFile)

    const result = sourceFile.getFullText()

    expect(result).toBe(expected)
  })

  test("serverComponent: intl.formatMessage with ternary inside", () => {
    const input = readFileSync(
      join(__dirname, "./fixtures/serverComponentWithTernaryInside.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const expected = readFileSync(
      join(__dirname, "./expectations/serverComponentWithTernaryInside.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const project = new Project({
      manipulationSettings: {
        indentationText: IndentationText.TwoSpaces,
      },
    })
    const sourceFile = project.createSourceFile(
      "./fixtures/serverComponentWithTernaryInside.tsx",
      input
    )

    processSourceFile(sourceFile)

    const result = sourceFile.getFullText()
    expect(result).toBe(expected)
  })

  test("serverComponent: intl.formatMessage with variables", () => {
    const input = readFileSync(
      join(__dirname, "./fixtures/serverComponentWithVariables.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const expected = readFileSync(
      join(__dirname, "./expectations/serverComponentWithVariables.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const project = new Project({
      manipulationSettings: {
        indentationText: IndentationText.TwoSpaces,
      },
    })
    const sourceFile = project.createSourceFile(
      "./fixtures/serverComponentWithVariables.tsx",
      input
    )

    processSourceFile(sourceFile)

    const result = sourceFile.getFullText()
    expect(result).toBe(expected)
  })

  test("serverComponent: intl.formatMessage with variable for id", () => {
    const input = readFileSync(
      join(__dirname, "./fixtures/serverComponentVariableId.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const expected = readFileSync(
      join(__dirname, "./expectations/serverComponentVariableId.tsx"),
      {
        encoding: "utf-8",
      }
    )

    const project = new Project({
      manipulationSettings: {
        indentationText: IndentationText.TwoSpaces,
      },
    })
    const sourceFile = project.createSourceFile(
      "./fixtures/serverComponentVariableId.tsx",
      input
    )

    processSourceFile(sourceFile)

    const result = sourceFile.getFullText()
    expect(result).toBe(expected)
  })
})
