import { extractModelId, extractNumber } from "../system/ultils.js";
import { ScrapModel } from "./scrap-model.js";

export class LangtonsScrapModel extends ScrapModel {
  action = async () => {
    const urlsData = this.extractUrls();

    for (let item of urlsData) {
      await this.page.goto(item.url);

      const data = await this.getItemsInHtml(item);

      const results = this.filterItemByKeyword(item.keyword, data);

      this.results[item.keyword] = results;

      console.log({ results: this.results });
    }
  };

  getItemsInHtml = async (data) => {
    const elements = await this.page.$$(".row.product-grid.grid-view");
    const results = [];

    for (const el of elements) {
      const url = await el
        .$eval("a.js-pdp-link.pdp-link-anchor", (el) => el.getAttribute("href"))
        .catch(() => null);

      const image_url = await el
        .$eval("img.tile-image.loaded", (img) => img.getAttribute("src"))
        .catch(() => null);

      const name = await el
        .$eval(".link.js-pdp-link", (el) => el.textContent.trim())
        .catch(() => null);

      const current_price = await el
        .$eval("div.max-bid-price.price", (el) => el.textContent.trim())
        .catch(() => null);

      results.push({
        url: `${this.web_bid.origin_url}${url}`,
        image_url,
        name,
        keyword: data.keyword,
        model: extractModelId(`${this.web_bid.origin_url}${url}`),
        current_price: extractNumber(current_price),
        scrap_config_id: this.scrap_config_id,
      });
    }

    return results;
  };
}
