import filteredLokalise from "./translations/en.json" with { type: "json" }
import allLokalise from "./translations-all/en.json" with { type: "json" }
import fromCodebase from "./extracted.json" with { type: "json" }

function diffArray(json1, json2) {
  const diff = []
  const keys1 = Object.keys(json1)
  const keys2 = Object.keys(json2)

  keys1.forEach((key) => {
    if (!keys2.includes(key)) {
      diff.push(key)
    }
  })

  return diff
}

function resolveLabels(ids, arr) {
  return ids.map((id) => {
    return {
      id,
      ...arr[id],
    }
  })
}

const labelsHidden = diffArray(allLokalise, filteredLokalise)
const labelsToRemove = diffArray(filteredLokalise, fromCodebase)
const labelsToAdd = diffArray(fromCodebase, filteredLokalise).filter(
  (key) => !labelsHidden.includes(key)
)

if (labelsToRemove.length === 0 && labelsToAdd.length === 0) {
  console.log(`Nothing has changed!`)
} else {
  console.log(`Labels to REMOVE from Lokalise: ${labelsToRemove.length}`)
  console.log(`Labels to ADD to Lokalise: ${labelsToAdd.length}`)
  console.log(`Labels HIDDEN in Lokalise: ${labelsHidden.length}`)
  console.log("")
}

if (labelsToRemove.length) {
  console.log(`${labelsToRemove.length} labels to remove from Lokalise:`)
  console.table(resolveLabels(labelsToRemove, filteredLokalise))
  console.log("")
}

if (labelsToAdd.length) {
  console.log("")
  console.log(`${labelsToAdd.length} labels to add to Lokalise`)
  console.table(resolveLabels(labelsToAdd, fromCodebase))
  console.log("")
}

if (labelsHidden.length) {
  console.log("")
  console.log(`${labelsHidden.length} labels are hidden in Lokalise`)
  console.table(resolveLabels(labelsHidden, allLokalise))
  console.log("")
}

if (labelsToRemove.length === 0 && labelsToAdd.length === 0) {
  console.log(`Nothing has changed!`)
} else {
  console.log(`Labels to REMOVE from Lokalise: ${labelsToRemove.length}`)
  console.log(`Labels to ADD to Lokalise: ${labelsToAdd.length}`)
  console.log(`Labels HIDDEN in Lokalise: ${labelsHidden.length}`)
  console.log("")
}
