module Project
  module ProjectionsCommon
    include PageObject

    button(:territories_button, class: "MarketingHighlightsTable-territories-button")
    text_fields(:spotify_value, id: /spotify-projection/)
    text_fields(:apple_value, id: /apple-music-projection/)
    text_fields(:downloads_value, id: /downloads-projection/)
    text_fields(:other_value, id: /other-projection/)
    text_field(:spotify_global) { spotify_value_elements[0] }
    text_field(:apple_global) { apple_value_elements[0] }
    text_field(:downloads_global) { downloads_value_elements[0] }
    text_field(:other_global) { other_value_elements[0] }

    # territories modal
    divs(:territories, class: "AddHighlightProjectionModal-territories-list-item")
    button(:add, id: "confirm-add")

    elements(:delete_region_highlights, class: "Icons-trash-can")
    button(:confirm_delete, class: "MarketingHighlightsTable-delete-popover-confirm")

    def add_priority_territory(territory_name)
      territories_button_element.when_present.click
      wait_until { territories_elements.any? }
      territory_result = territories_elements.find do |territory|
        territory.label_element.text.include? territory_name
      end
      territory_result.label_element.click
      add
    end

    def wait_for_projection_data
      territories_button_element.when_present
      wait_until(5) { spotify_global != "" }
    end
  end
end
