import { TCModel, PurposeRestriction, RestrictionType } from '@iabtechlabtcf/core';
import { makeRandomInt } from './makeRandomInt.js';
import { GVLFactory } from './GVLFactory.js';
export class TCModelFactory {
    static noGVL(gvl) {
        const tcModel = this.createBaseTCModel(gvl);
        return tcModel;
    }
    static createBaseTCModel(gvl) {
        const latestGVL = gvl || GVLFactory.getLatest();
        const tcModel = new TCModel();
        tcModel.cmpId = makeRandomInt(2, 100);
        tcModel.cmpVersion = makeRandomInt(1, 10);
        tcModel.consentScreen = makeRandomInt(1, 5);
        tcModel.isServiceSpecific = !!makeRandomInt(0, 1);
        tcModel.vendorListVersion = makeRandomInt(1, latestGVL.vendorListVersion);
        let counter = 0;
        const rand = makeRandomInt(1, TCModel.consentLanguages.size);
        TCModel.consentLanguages.forEach((lang) => {
            counter++;
            if (counter === rand) {
                tcModel.consentLanguage = lang;
            }
        });
        tcModel.publisherCountryCode = String.fromCharCode(makeRandomInt(65, 90)) +
            String.fromCharCode(makeRandomInt(65, 90));
        const date = new Date();
        const utcDate = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
        const now = utcDate.getTime();
        const GDPRMageddon = 1576883249;
        tcModel.created = new Date(makeRandomInt(GDPRMageddon, now));
        tcModel.lastUpdated = new Date(makeRandomInt(GDPRMageddon, now));
        const mapping = {
            'purposes': [
                'publisherConsents',
                'publisherLegitimateInterests',
                'purposeConsents',
                'purposeLegitimateInterests',
            ],
            'specialFeatures': [
                'specialFeatureOptins',
            ],
            'vendors': [
                'vendorConsents',
                'vendorLegitimateInterests',
                'vendorsAllowed',
                'vendorsDisclosed',
            ],
        };
        Object.keys(mapping).forEach((gvlIntMap) => {
            const ids = Object.keys(latestGVL[gvlIntMap]).map((strId) => +strId);
            ids.forEach((id) => {
                mapping[gvlIntMap].forEach((vectorName) => {
                    // 75% chance of being set
                    if (makeRandomInt(1, 4) !== 3) {
                        tcModel[vectorName].set(id);
                    }
                });
            });
        });
        return tcModel;
    }
    static addPublisherRestrictions(tcModel, gvl) {
        if (!tcModel.gvl) {
            tcModel.gvl = gvl || GVLFactory.getLatest();
        }
        Object.keys(tcModel.gvl.vendors).forEach((vendorId) => {
            const vendor = tcModel.gvl.vendors[vendorId];
            if (vendor.flexiblePurposes.length) {
                const purposeId = vendor.flexiblePurposes[makeRandomInt(0, vendor.flexiblePurposes.length - 1)];
                const isInConsent = vendor.purposes.includes(purposeId);
                const notAllowed = makeRandomInt(0, 1) === 1;
                let purpRestriction;
                if (notAllowed) {
                    purpRestriction = new PurposeRestriction(purposeId, RestrictionType.NOT_ALLOWED);
                }
                else if (isInConsent) {
                    purpRestriction = new PurposeRestriction(purposeId, RestrictionType.REQUIRE_LI);
                }
                else {
                    purpRestriction = new PurposeRestriction(purposeId, RestrictionType.REQUIRE_CONSENT);
                }
                tcModel.publisherRestrictions.add(+vendorId, purpRestriction);
            }
        });
        return tcModel;
    }
    static withGVL(gvl) {
        const tcModel = this.createBaseTCModel(gvl);
        tcModel.gvl = gvl || GVLFactory.getLatest();
        return tcModel;
    }
}
