{"size": 884, "ext": "js", "max_stars_repo_path": "src/js/imagesClass.js", "max_stars_repo_name": "DenisSgh/goit-js-hw-13-image-finder", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/js/imagesClass.js", "max_issues_repo_name": "DenisSgh/goit-js-hw-13-image-finder", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/js/imagesClass.js", "max_forks_repo_name": "DenisSgh/goit-js-hw-13-image-finder", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.0909090909, "max_line_length": 122, "alphanum_fraction": 0.5814479638}
import { successRequest, invalidRequest } from './notifications.js';

const BASE_URL =
  'https://pixabay.com/api/?key=21819092-c619300407dec0bd926653a16';

export default class Images {
  constructor() {
    this.searchQuery = '';
    this.page = 1;
  }

  async fetchImages() {
    const url = `${BASE_URL}&image_type=photo&orientation=horizontal&q=${this.searchQuery}&page=${this.page}&per_page=12`;

    return await fetch(url)
      .then(resp => resp.json())
      .then(({ hits }) => {
        if (hits.length === 0) {
          return invalidRequest();
        } else {
          this.incrementPage();
          successRequest();
          return hits;
        }
      });
  }

  incrementPage() {
    this.page += 1;
  }

  resetPage() {
    this.page = 1;
  }

  get query() {
    return this.searchQuery;
  }

  set query(newQuery) {
    this.searchQuery = newQuery;
  }
}
