{"size": 1835, "ext": "js", "max_stars_repo_path": "lib/search.js", "max_stars_repo_name": "diosamuel/Senginta.js", "max_stars_repo_stars_event_min_datetime": "2021-06-27T15:50:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-25T10:38:10.000Z", "max_issues_repo_path": "lib/search.js", "max_issues_repo_name": "diosamuel/Senginta.js", "max_issues_repo_issues_event_min_datetime": "2021-07-26T05:12:28.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-02T14:23:06.000Z", "max_forks_repo_path": "lib/search.js", "max_forks_repo_name": "diosamuel/Senginta.js", "max_forks_repo_forks_event_min_datetime": "2021-10-01T23:52:01.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-01T23:52:01.000Z"}
{"max_stars_count": 5, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 22.9375, "max_line_length": 82, "alphanum_fraction": 0.629972752}
const { XMLHttpRequest } = require('xmlhttprequest')

const { is_start_page_small } = require('./middleware')

String.prototype.format = function() {
	var args = arguments
	return this.replace(/\{(\d+)\}/g, function (m, i) {
		return args[i]
	})
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

class Search{
	constructor(search_word, start_page=1, max_page=1) {
		is_start_page_small(start_page, max_page)
		this.search_word = search_word
		this.start_page = start_page
		this.max_page = max_page

		this.sleep = []
		this.obj_of_td = {}
		this.htmlResponse = {}

		this.search_entries = {}
	}

	async get_result(url, page_num, max_page_num) {
		if (Object.keys(this.htmlResponse).length > 0) {
			return this.htmlResponse
		}

		let num = this.start_page
		await (async () => { 
			while (page_num <= max_page_num) {
				const search_url = url.format(this.search_word, page_num)

				const page = new XMLHttpRequest()
				page.open( "GET", search_url, false )
				await page.send()

				Object.assign(this.htmlResponse, {[num]: page.responseText})
				page_num += 10
				num += 1

				const msSleep = Math.random() * 2000
				await sleep(msSleep)
				this.sleep.push(msSleep)
			}
		})()

		return this.htmlResponse
	}

	to_obj(labels, ...datas) {
		if (Object.keys(this.search_entries).length > 0) { return this.search_entries }

		this.search_entries = {}
		for (let i=0; i < datas[0].length; i++) {
			const entry = {}

			let labelID = 0
			for (const data of datas) {
				const label = labels[labelID]
				const value = data[i]
				
				Object.assign(entry, { [label]: value })
				labelID++
			}

			Object.assign(this.search_entries, {[i]: entry})
		}

		return this.search_entries
	}
}

module.exports = Search