# typed: strict
class Domain::Fa::Parser::UserListParserHelper
  extend T::Sig

  User = Struct.new(:name, :url_name, :href, keyword_init: true)

  sig { params(page: Nokogiri::XML::Node).returns(T::Array[User]) }
  def self.user_list(page)
    page
      .css(".watch-list .watch-list-items")
      .map { |elem| watch_list_item_to_user_struct(elem) }
  end

  private

  sig { params(elem: Nokogiri::XML::Node).returns(User) }
  def self.watch_list_item_to_user_struct(elem)
    link = elem.css("a").first
    href = link["href"]
    raise unless href.starts_with?("/user/")
    # strip off '/user/' prefix
    url_name = href.split("/").reject(&:blank?).last

    User.new(name: link.text, url_name: url_name, href: href)
  end
end
