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

  sig { returns(T::Boolean) }
  attr_accessor :debug

  sig { params(elem: Nokogiri::XML::Node, page_version: Symbol).void }
  def initialize(elem, page_version)
    @new_parse_mode = T.let(!!elem.css("figcaption").first, T::Boolean)
    @elem = elem
    @page_version = page_version
    @debug = T.let(false, T::Boolean)

    @id = T.let(nil, T.nilable(Integer))
    @artist = T.let(nil, T.nilable(String))
    @artist_user_page_path = T.let(nil, T.nilable(String))
    @title = T.let(nil, T.nilable(String))
    @view_path = T.let(nil, T.nilable(String))
    @thumb_path = T.let(nil, T.nilable(String))
    @fav_id = T.let(nil, T.nilable(Integer))
  end

  sig { returns(T.nilable(Integer)) }
  def id
    @id ||= %r{/view/(\d+)}.match(view_path).try(:[], 1).try(:to_i)
  end

  sig { returns(T.nilable(String)) }
  def artist
    @artist ||=
      if !@new_parse_mode
        @elem.css("small a").first.try(:text)
      else
        @elem.css("figcaption p").last.css("a").text
      end
  end

  sig { returns(T.nilable(String)) }
  def artist_user_page_path
    @artist_user_page_path ||=
      if !@new_parse_mode
        @elem.css("small a").first.try(:[], "href")
      else
        @elem.css("figcaption p").last.css("a").first["href"]
      end
  end

  sig { returns(T.nilable(String)) }
  def artist_url_name
    artist_user_page_path&.split("/")&.last
  end

  sig { returns(T.nilable(String)) }
  def title
    @title ||=
      if !@new_parse_mode
        @elem.css("> span").first["title"]
      else
        @elem.css("figcaption p").first.css("a").first["title"]
      end
  end

  sig { returns(T.nilable(String)) }
  def view_path
    @view_path ||=
      if !@new_parse_mode
        @elem.css("u s a").first["href"]
      else
        @elem.css("b u a").first["href"]
      end
  end

  sig { returns(T.nilable(String)) }
  def thumb_path
    @thumb_path ||=
      if !@new_parse_mode
        @elem.css("u s a img").first["src"]
      else
        @elem.css("b u a img").first["src"]
      end
  end

  sig { returns(T.nilable(Integer)) }
  def fav_id
    @fav_id ||=
      begin
        @elem.attr("data-fav-id")&.to_i
      end
  end
end
