defmodule Movement.Mappers.Operation do
  @moduledoc false
  alias Accent.PreviousTranslation

  @spec map(binary, map, map) :: Movement.Operation.t()
  def map("new" = action, current_translation, suggested_translation) do
    %Movement.Operation{
      action: action,
      text: suggested_translation.text,
      key: suggested_translation.key,
      file_comment: suggested_translation.file_comment,
      file_index: suggested_translation.file_index,
      value_type: suggested_translation.value_type,
      plural: suggested_translation.plural,
      locked: suggested_translation.locked,
      revision_id: Map.get(suggested_translation, :revision_id),
      document_id: Map.get(suggested_translation, :document_id),
      version_id: Map.get(suggested_translation, :version_id),
      translation_id: Map.get(suggested_translation, :translation_id),
      previous_translation: PreviousTranslation.from_translation(current_translation),
      machine_translations_enabled: suggested_translation.machine_translations_enabled,
      placeholders: suggested_translation.placeholders
    }
  end

  def map(action, current_translation, suggested_translation) do
    %Movement.Operation{
      action: action,
      text: suggested_translation.text,
      key: Map.get(suggested_translation, :key, current_translation.key),
      file_comment: Map.get(suggested_translation, :file_comment, current_translation.file_comment),
      file_index: Map.get(suggested_translation, :file_index, current_translation.file_index),
      document_id: Map.get(suggested_translation, :document_id, current_translation.document_id),
      revision_id: Map.get(suggested_translation, :revision_id, current_translation.revision_id),
      version_id: Map.get(suggested_translation, :version_id, current_translation.version_id),
      value_type: Map.get(suggested_translation, :value_type, current_translation.value_type),
      plural: Map.get(suggested_translation, :plural, current_translation.plural),
      locked: Map.get(suggested_translation, :locked, current_translation.locked),
      translation_id: Map.get(current_translation, :id),
      machine_translations_enabled: Map.get(suggested_translation, :machine_translations_enabled, false),
      previous_translation: PreviousTranslation.from_translation(current_translation),
      placeholders: Map.get(suggested_translation, :placeholders, current_translation.placeholders)
    }
  end
end
