'use strict';

const bhttp = require('bhttp');
const cheerio = require('cheerio');

const {
    scrapeLatestX,
    fetchLatest,
    fetchScene,
    fetchProfile,
} = require('./mindgeek');

function scrapeLatestClassic(html, site) {
    const $ = cheerio.load(html, { normalizeWhitespace: true });

    const stateTag = $('script:contains("initialState")').html();
    const prefix = 'initialState = {';
    const prefixIndex = stateTag.indexOf('initialState = {');
    const suffix = '};';
    const stateString = stateTag.slice(prefixIndex + prefix.length - 1, stateTag.indexOf('};', prefixIndex) + suffix.length - 1);
    const data = JSON.parse(stateString);

    return Object.values(data.entities.releases).map(scene => scrapeLatestX(scene, site));
}

async function fetchClassic(site, page) {
    const res = await bhttp.get(`${site.url}/scenes?page=${page}`);

    if (res.statusCode === 200) {
        return scrapeLatestClassic(res.body.toString(), site);
    }

    return null;
}

async function fetchLatestWrap(site, page = 1) {
    if (site.parameters?.classic) {
        return fetchClassic(site, page);
    }

    return fetchLatest(site, page);
}

async function networkFetchProfile(actorName) {
    return fetchProfile(actorName, 'realitykings');
}

module.exports = {
    fetchLatest: fetchLatestWrap,
    fetchProfile: networkFetchProfile,
    fetchScene,
};
