'use strict';

const { fetchLatest, fetchScene } = require('./julesjordan');

function extractActors(scene) {
    const release = scene;

    if (!scene.actors || scene.actors.length === 0) {
        const introActorMatches = scene.title.match(/(?:presents|introduces|features|welcomes) (\w+ \w+)/i);
        const introTwoActorMatches = scene.title.match(/(?:presents|introduces|features|welcomes) (?:(\w+)|(\w+ \w+)) and (\w+ \w+)/i);
        const returnActorMatches = scene.title.match(/(?:(^\w+)|(\w+ \w+))(?:,| (?:return|visit|pov|give|suck|lick|milk|love|enjoy|service|is))/i);
        const returnTwoActorMatches = scene.title.match(/(\w+ \w+) and (?:(\w+)|(\w+ \w+)) (?:return|visit|give|suck|lick|milk|love|enjoy|service|are)/i);

        const rawActors = (introTwoActorMatches || introActorMatches || returnTwoActorMatches || returnActorMatches)?.slice(1);
        const actors = rawActors?.filter((actor) => {
            if (!actor) return false;
            if (/swallow|\bcum|fuck|suck|give|giving|take|takes|taking|head|teen|babe|cute|beaut|naughty|teacher|nanny|adorable|brunette|blonde|bust|audition|from|\band\b|\bto\b/i.test(actor)) return false;

            return true;
        });

        if (actors) {
            release.actors = actors;
        }
    }

    if (release.actors?.length > 1 || /threesome|threeway/.test(scene.title)) {
        release.tags = scene.tags ? [...scene.tags, 'mff'] : ['mff'];
    }

    return release;
}

async function fetchLatestWrap(site, page = 1) {
    const latest = await fetchLatest(site, page);

    return latest.map(scene => extractActors(scene));
}

async function fetchSceneWrap(url, site) {
    const scene = await fetchScene(url, site);

    return extractActors(scene);
}

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