# frozen_string_literal: true

module Mina
  class Configuration
    include Singleton
    include Helpers::Internal

    module DSL
      def self.included(base)
        [:set, :fetch, :remove, :set?, :ensure!, :reset!].each do |method|
          base.send :define_method, method do |*args, &block|
            Configuration.instance.send(method, *args, &block)
          end
        end
      end
    end

    attr_reader :variables

    def initialize
      @variables = {}
    end

    def set(key, value = nil, &block)
      variables[key] = block || value
    end

    def fetch(key, default = nil)
      value = ENV[key.to_s] || variables.fetch(key, default)
      value.respond_to?(:call) ? value.call : value
    end

    def remove(key)
      variables.delete(key)
    end

    def set?(key)
      ENV.key?(key.to_s) || !variables.fetch(key, nil).nil?
    end

    def ensure!(key, message: nil)
      return if set?(key)

      message ||= "#{key} must be defined!"
      error! message
    end

    def reset!
      @variables = {}
    end
  end
end
