// ===== Configuration ===== const API_BASE = ""; // ===== DOM Elements ===== const form = document.getElementById("researchForm"); const categoryInput = document.getElementById("productCategory"); const descriptionInput = document.getElementById("productDescription"); const submitBtn = document.getElementById("submitBtn"); const btnText = submitBtn.querySelector(".btn-text"); const btnLoader = submitBtn.querySelector(".btn-loader"); const errorBanner = document.getElementById("errorBanner"); const resultsDiv = document.getElementById("results"); const toggleBtns = document.querySelectorAll(".toggle-btn"); // Multi-select elements const multiselect = document.getElementById("audienceMultiselect"); const selectedContainer = document.getElementById("selectedAudiences"); const dropdown = document.getElementById("audienceDropdown"); const searchInput = document.getElementById("audienceSearch"); const optionsContainer = document.getElementById("audienceOptions"); let selectedMethod = "gpt"; let selectedAudiences = []; let allAudiences = []; // ===== Load Target Audiences ===== async function loadAudiences() { try { const res = await fetch(`${API_BASE}/api/target-audiences`); if (!res.ok) throw new Error("Failed to load audiences"); const data = await res.json(); allAudiences = data.audiences; renderOptions(); renderSelected(); } catch (err) { console.error("Could not load audiences:", err); selectedContainer.innerHTML = '⚠ Could not load — is the backend running?'; } } // ===== Multi-select Logic (UNCHANGED) ===== function renderOptions(filter = "") { const filterLower = filter.toLowerCase(); const filtered = allAudiences.filter((a) => a.toLowerCase().includes(filterLower) ); optionsContainer.innerHTML = filtered .map((a) => { const isSelected = selectedAudiences.includes(a); return `
Ad Angles
Ad Concepts