let snInstallTag = document.querySelector(`[src="https://scripts.streamlinedealer.com"]`);
let snGroup = snInstallTag.getAttribute("data-group") || "default";
let snTag = snInstallTag.getAttribute("data-tag") || "none";
let snDomain = snInstallTag.getAttribute("data-domain");
if(!snDomain && window.location.href.includes(snGroup))snDomain = snGroup.split(".").slice(-2).join(".");

let bypassMap = {
    "frank-fletcher-subaru":"fletchersubaru.com",
    "frank-fletcher-honda":"fletcherhonda.com",
    "frank-fletcher-chrysler-jeep":"frankfletcherdodgechryslerjeep.com",
    "frank-fletcher-ford-lincoln":"frankfletcherford.net",
    "frank-fletcher-subaru":"fletchersubaru.com",
    "frank-fletcher-cjdr-arkansas":"fletcherjonesboro.com",
    "frank-fletcher-superstore-hyundai":"fletcherhyundai.com",
    "fletcher-superstore":"fletcherjoplin.com",
    "frank-fletcher-toyota":"fletcher-toyota.com"
}


if(snDomain = bypassMap[snGroup])loadSnSpecialsContainer(snDomain,snTag);
else loadLegacyScript();

async function loadLegacyScript(){
    let el = document.createElement("script");
    el.src = "https://scripts.streamlinedealer.com/group/";
    document.head.appendChild(el);
}

async function loadSnSpecialsContainer(domain,tag){
    let json = await fetch(`https://specials.streamlinedealer.com/api/html/${snDomain}/${snTag}/`).then(response => response.json());
    if(json.html){
        let parent = snInstallTag.parentNode;
        let section = document.createElement("section");
        section.innerHTML = json.html;
        parent.insertBefore(section, snInstallTag);
    }
    if(json.css){
        let style = document.createElement("style");
        style.innerHTML = json.css;
        document.head.appendChild(style);
    }
}