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

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

    for (let item of urlsData) {
      const { data } = await axios({
        url: item.url,
        method: "POST",
        data: {
          top: 50,
          skip: 0,
          sort: {},
          searchText: item.keyword,
          filter: {
            Display: true,
          },
          dropship: false,
        },
      });

      const auctions = data?.auctions || [];

      const mappedData = auctions.map((auction) => {
        return {
          url: auction.AucDetailsUrlLink,
          image_url: auction.aucThumbnailUrl,
          name: auction.aucTitle,
          keyword: item.keyword,
          model: extractModelId(auction.AucDetailsUrlLink),
          current_price: auction.aucCurrentBid,
          scrap_config_id: this.scrap_config_id,
        };
      });

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

      this.results[item.keyword] = results;

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