(function (root) { 'use strict'; function scopedStorage(storage, clientId) { if (!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(clientId || '')) throw new Error('Verified client identity required'); const prefix = 'aura-studio:' + clientId.toLowerCase() + ':'; return Object.freeze({ getItem(key) { return storage.getItem(prefix + key); }, setItem(key, value) { storage.setItem(prefix + key, value); }, removeItem(key) { storage.removeItem(prefix + key); } }); } function escapeText(value) { return String(value ?? '').replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[char])); } function whatsappUrl(phone) { const digits = String(phone || '').replace(/[\s().-]/g, '').replace(/^\+/, ''); return /^[1-9]\d{6,14}$/.test(digits) ? 'https://wa.me/' + digits : ''; } if (typeof module === 'object' && module.exports) { module.exports = { scopedStorage, escapeText, whatsappUrl }; return; } async function start() { const gate = document.getElementById('studioWorkspaceGate'); const status = document.getElementById('workspaceStatus'); const form = document.getElementById('workspaceForm'); const tenantInput = document.getElementById('workspaceTenant'); const login = document.getElementById('workspaceLogin'); const retry = document.getElementById('workspaceRetry'); const params = new URLSearchParams(location.search); const tenant = params.get('tenant') || ''; tenantInput.value = tenant; document.querySelectorAll('body > :not(#studioWorkspaceGate):not(script)').forEach(el => el.inert = true); retry.onclick = () => location.reload(); form.onsubmit = event => { event.preventDefault(); const next = new URL(location.href); next.searchParams.set('tenant', tenantInput.value.trim()); location.assign(next.href); }; try { const response = await fetch('/api/workspace' + (tenant ? '?tenant=' + encodeURIComponent(tenant) : ''), { credentials: 'same-origin', cache: 'no-store' }); const data = await response.json(); if (!response.ok || data.success !== true) { status.textContent = data.error || 'Workspace যাচাই করা যায়নি।'; if (data.code === 'WORKSPACE_REQUIRED') form.hidden = false; if (response.status === 401) { const configResponse = await fetch('/api/studio-config', { cache: 'no-store' }); const config = await configResponse.json(); if (configResponse.ok && config.loginUrl) { const target = new URL(config.loginUrl); if (!['https:', 'http:'].includes(target.protocol) || target.username || target.password) throw new Error('Invalid login destination'); target.searchParams.set('next', location.href); login.href = target.href; login.hidden = false; } } return; } const workspace = data.workspace; const runtime = { workspace: Object.freeze({ ...workspace }), local: scopedStorage(localStorage, workspace.clientId), session: scopedStorage(sessionStorage, workspace.clientId), escapeText, whatsappUrl, apiUrl(path) { const url = new URL(path, location.origin); if (url.origin !== location.origin || !url.pathname.startsWith('/api/')) throw new Error('Invalid Studio API URL'); url.searchParams.set('tenant', workspace.tenant); return url.pathname + url.search; } }; Object.defineProperty(window, 'AuraStudio', { value: Object.freeze(runtime), writable: false }); const script = document.createElement('script'); script.src = '/app.js?v=workspace-1'; script.onerror = () => { status.textContent = 'Studio load হয়নি। Refresh করে আবার চেষ্টা করুন।'; }; script.onload = () => { try { document.dispatchEvent(new Event('aura-studio-ready')); document.querySelectorAll('body > :not(#studioWorkspaceGate):not(script)').forEach(el => el.inert = false); gate.hidden = true; const brandForm = document.getElementById('workspaceBrandForm'); const brandStatus = document.getElementById('workspaceBrandStatus'); if (brandForm) { const brand = workspace.brand || { name: workspace.name, website: '', phone: '' }; ['name', 'website', 'phone'].forEach(key => { brandForm.elements[key].value = brand[key] || ''; }); brandForm.querySelector('button').disabled = !workspace.canWriteProducts; brandForm.onsubmit = async event => { event.preventDefault(); const submit = brandForm.querySelector('button'); submit.disabled = true; brandStatus.textContent = 'সংরক্ষণ হচ্ছে…'; try { const result = await fetch(runtime.apiUrl('/api/brand'), { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(Object.fromEntries(['name', 'website', 'phone'].map(key => [key, brandForm.elements[key].value]))) }); const saved = await result.json(); if (!result.ok || saved.success !== true) throw new Error(saved.error || 'সংরক্ষণ হয়নি।'); location.reload(); } catch (error) { brandStatus.textContent = error.message; submit.disabled = !workspace.canWriteProducts; } }; } const button = document.createElement('button'); button.type = 'button'; button.className = 'pill-btn'; button.textContent = workspace.name || workspace.tenant; button.title = 'Workspace নির্বাচন'; button.onclick = () => { gate.hidden = false; form.hidden = false; status.textContent = 'অন্য workspace নির্বাচন করুন।'; }; document.querySelector('.header-actions')?.prepend(button); } catch { status.textContent = 'Studio চালু হয়নি। Refresh করে আবার চেষ্টা করুন।'; } }; document.body.appendChild(script); } catch { status.textContent = 'Workspace service-এর উত্তর পাওয়া যায়নি। Refresh করে আবার চেষ্টা করুন।'; } } document.addEventListener('DOMContentLoaded', start, { once: true }); })(typeof globalThis === 'object' ? globalThis : this);