export interface DesignTokens { primaryColor: string backgroundPrimary: string backgroundSecondary: string borderRadius: number font: string selectedPalette: string } export function encodeDesignTokens(tokens: DesignTokens): string { try { const encoded = btoa(JSON.stringify(tokens)) return encoded } catch (error) { console.error("Failed to encode design tokens:", error) return "" } } export function decodeDesignTokens(encoded: string): DesignTokens | null { try { const decoded = atob(encoded) const tokens = JSON.parse(decoded) // Validate the structure if ( typeof tokens.primaryColor === "string" && typeof tokens.backgroundPrimary === "string" && typeof tokens.backgroundSecondary === "string" && typeof tokens.borderRadius === "number" && typeof tokens.font === "string" && typeof tokens.selectedPalette === "string" ) { return tokens } return null } catch (error) { console.error("Failed to decode design tokens:", error) return null } } export function generateShareUrl(tokens: DesignTokens): string { const encoded = encodeDesignTokens(tokens) if (!encoded) return window.location.href const url = new URL(window.location.href) url.searchParams.set("design", encoded) return url.toString() } export function copyToClipboard(text: string): Promise { if (navigator.clipboard && window.isSecureContext) { return navigator.clipboard .writeText(text) .then(() => true) .catch(() => false) } else { // Fallback for older browsers const textArea = document.createElement("textarea") textArea.value = text textArea.style.position = "fixed" textArea.style.left = "-999999px" textArea.style.top = "-999999px" document.body.appendChild(textArea) textArea.focus() textArea.select() try { const successful = document.execCommand("copy") document.body.removeChild(textArea) return Promise.resolve(successful) } catch (err) { document.body.removeChild(textArea) return Promise.resolve(false) } } }