module Spoom::Sorbet::Sigils

def change_sigil_in_file(path, new_strictness)

: ((String | Pathname) path, String new_strictness) -> bool
changes the sigil in the file at the passed path to the specified new strictness
def change_sigil_in_file(path, new_strictness)
  content = File.read(path, encoding: Encoding::ASCII_8BIT)
  new_content = update_sigil(content, new_strictness)
  File.write(path, new_content, encoding: Encoding::ASCII_8BIT)
  strictness_in_content(new_content) == new_strictness
end

def change_sigil_in_files(path_list, new_strictness)

: (Array[String] path_list, String new_strictness) -> Array[String]
changes the sigil to have a new strictness in a list of files
def change_sigil_in_files(path_list, new_strictness)
  path_list.filter do |path|
    change_sigil_in_file(path, new_strictness)
  end
end

def file_strictness(path)

: ((String | Pathname) path) -> String?
* returns nil if no sigil
returns a string containing the strictness of a sigil in a file at the passed path
def file_strictness(path)
  return unless File.file?(path)
  content = File.read(path, encoding: Encoding::ASCII_8BIT)
  strictness_in_content(content)
end

def sigil_string(strictness)

: (String strictness) -> String
returns the full sigil comment string for the passed strictness
def sigil_string(strictness)
  "# typed: #{strictness}"
end

def strictness_in_content(content)

: (String content) -> String?
returns the strictness of a sigil in the passed file content string (nil if no sigil)
def strictness_in_content(content)
  SIGIL_REGEXP.match(content)&.[](1)
end

def update_sigil(content, new_strictness)

: (String content, String new_strictness) -> String
returns a string which is the passed content but with the sigil updated to a new strictness
def update_sigil(content, new_strictness)
  content.sub(SIGIL_REGEXP, sigil_string(new_strictness))
end

def valid_strictness?(strictness)

: (String strictness) -> bool
returns true if the passed string is a valid strictness (else false)
def valid_strictness?(strictness)
  VALID_STRICTNESS.include?(strictness.strip)
end