# typed: strict
# frozen_string_literal: true

require "cache_store"
require "formula_versions"

#
# {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used
# by the `brew desc` and `brew search` commands.
#
class DescriptionCacheStore < CacheStore
  Key = type_member { { fixed: String } }
  Value = type_member { { fixed: T.anything } }

  # Inserts a formula description into the cache if it does not exist or
  # updates the formula description if it does exist.
  #
  # @param formula_name [String] the name of the formula to set
  # @param description  [String] the description from the formula to set
  # @return [nil]
  sig { params(formula_name: String, description: T.anything).void }
  def update!(formula_name, description)
    database.set(formula_name, description)
  end

  # Delete the formula description from the {DescriptionCacheStore}.
  #
  # @param formula_name [String] the name of the formula to delete
  # @return [nil]
  sig { params(formula_name: String).void }
  def delete!(formula_name)
    database.delete(formula_name)
  end

  # If the database is empty `update!` it with all known formulae.
  #
  # @return [nil]
  sig { params(eval_all: T::Boolean).void }
  def populate_if_empty!(eval_all: Homebrew::EnvConfig.tap_trust_configured?)
    return unless eval_all
    return unless database.empty?

    Formula.all(eval_all:).each { |f| update!(f.full_name, f.desc) }
  end

  # Use an update report to update the {DescriptionCacheStore}.
  #
  # @param report [Report] an update report generated by cmd/update.rb
  # @return [nil]
  sig { params(report: ReporterHub).void }
  def update_from_report!(report)
    unless Homebrew::EnvConfig.tap_trust_configured?
      database.clear!
      return
    end
    return populate_if_empty! if database.empty?
    return if report.empty?

    renamings = T.cast(report.select_formula_or_cask(:R), T::Array[[String, String]])
    alterations = T.cast(
      report.select_formula_or_cask(:A) +
      report.select_formula_or_cask(:M) +
      renamings.filter_map(&:last),
      T::Array[String],
    )
    deletions = T.cast(
      report.select_formula_or_cask(:D) + renamings.filter_map(&:first),
      T::Array[String],
    )

    update_from_formula_names!(alterations)
    delete_from_formula_names!(deletions)
  end

  # Use an array of formula names to update the {DescriptionCacheStore}.
  #
  # @param formula_names [Array] the formulae to update
  # @return [nil]
  sig { params(formula_names: T::Array[String]).void }
  def update_from_formula_names!(formula_names)
    unless Homebrew::EnvConfig.tap_trust_configured?
      database.clear!
      return
    end
    return populate_if_empty! if database.empty?

    formula_names.each do |name|
      update!(name, Formula[name].desc)
    rescue FormulaUnavailableError, Homebrew::UntrustedTapError, *FormulaVersions::IGNORED_EXCEPTIONS
      delete!(name)
    end
  end

  # Use an array of formula names to delete them from the {DescriptionCacheStore}.
  #
  # @param formula_names [Array] the formulae to delete
  # @return [nil]
  sig { params(formula_names: T::Array[String]).void }
  def delete_from_formula_names!(formula_names)
    return if database.empty?

    formula_names.each { delete!(it) }
  end
  alias delete_from_cask_tokens! delete_from_formula_names!

  # `select` from the underlying database.
  sig { params(block: T.proc.params(arg0: Key, arg1: Value).returns(BasicObject)).returns(T::Hash[Key, Value]) }
  def select(&block)
    database.select(&block)
  end
end
require "description_cache_store/cask_description_cache_store"
