1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[7688],{3203:(e,t,r)=>{r.d(t,{_z:()=>a,dE:()=>l,qp:()=>n});let l=e=>{var t;return null!=(t=e.role)?t:"owned"},n=e=>{var t;return(null!=(t=null==e?void 0:e.productSlots)?t:[]).filter(e=>"owned"===l(e)&&!e.optional).length};function a(e,t){var r,n;let a=null!=e?e:[],o=null!=(r=null==t?void 0:t.productSlots)?r:[];return null!=(n=a.find(e=>"hook"===e.role&&(null==e.attachedSlot||!o[e.attachedSlot-1]||"owned"===l(o[e.attachedSlot-1]))))?n:a.find(e=>"hook"===e.role)}},58112:(e,t,r)=>{r.d(t,{FM:()=>o,mi:()=>a});var l=r(12115);let n=new Set;function a(e){let t=(e||"").trim().replace(/'/g,"");return/^[A-Za-z0-9 -]{1,40}$/.test(t)?"'".concat(t,"', sans-serif"):"sans-serif"}function o(e){let t=e.map(e=>{var t;return"".concat(e.family,"|").concat(null!=(t=e.weight)?t:700,"|").concat(+!!e.italic)}).join(",");(0,l.useEffect)(()=>{for(let t of e)!function(e){let{family:t,weight:r=700,italic:l=!1}=e;if("undefined"==typeof document)return;let a=(t||"").trim();if(!/^[A-Za-z0-9 -]{1,40}$/.test(a))return;let o=Math.min(900,Math.max(100,Math.round(r)||700)),i="".concat(a.toLowerCase(),"|").concat(o,"|").concat(+!!l);if(n.has(i))return;n.add(i);let u="/api/app2/assets/font?f=".concat(encodeURIComponent(a),"&w=").concat(o,"&i=").concat(+!!l),c=document.createElement("style");c.setAttribute("data-klp-font",i),c.textContent="@font-face{font-family:'".concat(a,"';font-weight:").concat(o,";font-style:").concat(l?"italic":"normal",";font-display:swap;src:url('").concat(u,"') format('truetype');}"),document.head.appendChild(c)}(t)},[t])}},67625:(e,t,r)=>{r.d(t,{FH:()=>G,Fn:()=>o,Fy:()=>k,PR:()=>N,Pi:()=>I,by:()=>w,c7:()=>y,gB:()=>a,hj:()=>H,tA:()=>U,yL:()=>x});var l=r(12115),n=r(3203);let a=8,o=4,i=["1:1","9:16","16:9"];function u(e,t){let r=Array.from(new Set((null!=e?e:[]).map(e=>"4:5"===e?"1:1":e).filter(e=>i.includes(e))));return r.length?r.slice(0,a):["4:5"===t?"1:1":t]}let c="with_model",s="clone",d="fresh";function f(){let e="undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2));return"custom-".concat(e)}function p(e,t){return t?"klipio:create-canvas:".concat(t):"klipio:create-canvas:".concat(e)}function v(e){return"completed"===e||"failed"===e||"dead_letter"===e}function m(e){var t;let r={...e,generating:!!(e.briefId&&!v(null==(t=e.build)?void 0:t.status)&&"generating"===e.status)},l=e=>{if(!e)return e;let{packageText:t,sourceComposition:r,...l}=e;return l},n=r.briefPayloads?Object.fromEntries(Object.entries(r.briefPayloads).map(e=>{let[t,r]=e;return[t,l(r)]})):void 0;return{...r,briefPayload:l(r.briefPayload),...n?{briefPayloads:n}:{}}}function g(e){return{...m(e),assets:[],build:null,builds:void 0}}function h(e){var t,r,l,n,a,o,i;let f=!!e.briefId&&!v(null==(t=e.build)?void 0:t.status)&&("generating"===e.status||e.generating),p=Array.isArray(e.selectedProducts)?e.selectedProducts:[],m=null!=(r="4:5"===e.aspectRatio?"1:1":e.aspectRatio)?r:"1:1",g=u(Array.isArray(e.sizes)&&e.sizes.length?e.sizes:[m],m),h=Array.isArray(e.laneProducts)&&e.laneProducts.length?e.laneProducts.map(e=>{var t;return{name:e.name,price:null!=(t=e.price)?t:null,images:Array.isArray(e.images)?e.images.filter(e=>"string"==typeof e):[],role:"hero"===e.role?"hero":"supporting",slotDescriptor:e.slotDescriptor}}):p.filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()}).map((t,r)=>{var l;return{name:t.name,price:0===r&&null!=(l=e.productPrice)?l:null,images:t.imageUrl?[t.imageUrl]:[],role:0===r?"hero":"supporting"}});return{...e,assets:Array.isArray(e.assets)?e.assets:[],selectedProducts:p,laneProducts:S(h),sizes:g,productModelMode:null!=(l=e.productModelMode)?l:c,outputMode:null!=(n=e.outputMode)?n:e.ref?s:d,aspectRatio:m,build:null!=(a=e.build)?a:null,generating:f,status:f?"generating":null!=(o=e.status)?o:"idle",briefReady:null!=(i=e.briefReady)?i:!!(e.briefId||"done"===e.status||e.builds&&Object.keys(e.builds).length),briefGenerating:!1,outputKinds:e.outputKinds&&"object"==typeof e.outputKinds?{creative:!1!==e.outputKinds.creative,plain:!0===e.outputKinds.plain}:{creative:!0,plain:!1}
1}}function b(e){var t,r,l,n,a,o;let i=(null!=(t=e.laneProducts)?t:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()}),u=null!=(r=i.find(e=>"hero"===e.role))?r:i[0],c=i.filter(e=>e!==u).map(e=>e.name),s=i.length?"":(null!=(l=e.selectedProducts)?l:[]).map(e=>e.name).join(",");return JSON.stringify({h:null!=(n=null==u?void 0:u.name)?n:"",p:null!=(a=null==u?void 0:u.price)?a:"",s:c,f:s,ip:null!=(o=e.includePrice)?o:null})}function y(e){return!!e.briefReady&&!!e.briefForKey&&e.briefForKey!==b(e)}function k(e){let t=e.outputKinds,r=[];return t&&!1===t.creative||r.push("creative"),(null==t?void 0:t.plain)===!0&&r.push("plain"),r.length?r:["creative"]}function I(e,t){var r;let l=null==(r=e.outputKindsBySize)?void 0:r[t];if(!l)return k(e);let n=[];return!1!==l.creative&&n.push("creative"),!0===l.plain&&n.push("plain"),n.length?n:["creative"]}function w(e,t){var r,l,n;return e.ref?null!=(n=null!=(l=null==(r=e.outputModeBySize)?void 0:r[t])?l:e.outputMode)?n:s:"fresh"}function C(e){return Array.isArray(e)?e.filter(e=>!!e&&"string"==typeof e.laneId).slice(0,12).map(h):[]}function A(e,t){var r;let l=P(t),n=R(t);return{laneId:"lane-".concat(e.adArchiveId),ref:e,productName:(null==t?void 0:t.name)||"",productImageUrl:l,productPrice:null!=(r=null==t?void 0:t.price)?r:null,selectedProducts:t?[{name:t.name,imageUrl:l}]:[],laneProducts:n,productLayout:"together",productModelMode:c,outputMode:s,aspectRatio:"1:1",sizes:["1:1"],outputCount:1,analysis:null,build:null,assets:[],generating:!1,status:"idle"}}function P(e){return e&&("shopify"===e.source||"firecrawl"===e.source)&&e.image?e.image:void 0}function M(e){return e&&("shopify"===e.source||"firecrawl"===e.source)?Array.from(new Set((Array.isArray(e.images)&&e.images.length?e.images:e.image?[e.image]:[]).filter(e=>"string"==typeof e&&/^https:\/\//i.test(e)))):[]}function S(e){if(0===e.length)return e;let t=e.findIndex(e=>"hero"===e.role),r=t>=0?t:0;return e.map((e,t)=>({...e,role:t===r?"hero":"supporting"}))}function R(e){var t;return(null==e?void 0:e.name)?[{name:e.name,price:null!=(t=e.price)?t:null,images:M(e),role:"hero"}]:[]}function z(e,t){return{...function(e,t){var r,l;let n=S(t.filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()})).slice(0,o),a=null!=(r=n.find(e=>"hero"===e.role))?r:n[0];return{laneProducts:n,selectedProducts:n.map(e=>({name:e.name,imageUrl:e.images[0]})),productName:(null==a?void 0:a.name)||"",productImageUrl:null==a?void 0:a.images[0],productPrice:null!=(l=null==a?void 0:a.price)?l:null}}(0,t),laneProducts:function(e){let t=!1,r=e.map(e=>{var r;return"hero"===e.role&&(null==(r=e.name)?void 0:r.trim())&&!t?(t=!0,e):"hero"===e.role?{...e,role:"supporting"}:e});if(!t){let e=r.findIndex(e=>{var t;return null==(t=e.name)?void 0:t.trim()});e>=0&&(r[e]={...r[e],role:"hero"})}return r}(t)}}function U(e,t){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return"".concat(e,"~").concat(t,"~").concat(r)}function N(e){let t=e.split("~"),r=t[0]||"4:5";return{size:r,kind:"plain"===t[1]?"plain":"creative",index:Number.isFinite(Number(t[2]))?Math.max(0,Math.trunc(Number(t[2]))):0}}function j(e){var t,r,l,n,a;let o,i=e.builds;if(!i||0===Object.keys(i).length)return{};let u=Object.values(i),c=u.some(e=>e.generating),s=Object.entries(i).flatMap(e=>{var t;let[r,l]=e;return(null!=(t=l.assets)?t:[]).map(e=>({...e,kind:N(r).kind}))}),d=u.find(e=>{var t;return!e.generating&&("failed"===e.status||(null==(t=e.build)?void 0:t.status)==="failed")}),f=null!=(l=null!=(r=null!=(t=null==d?void 0:d.build)?t:u.map(e=>e.build).find(e=>!!e))?r:e.build)?l:null,p=null!=(n=u.map(e=>e.jobId).find(Boolean))?n:e.jobId,v=null!=(a=u.map(e=>e.briefId).find(Boolean))?a:e.briefId;return o=c?"generating":s.length>0?"done":"failed",{build:f,assets:s,jobId:p,briefId:v,generating:c,status:o}}function O(e,t){let r=u(e,"1:1"),l=r.length?r:["1:1"],n=Math.max(1,Math.floor(a/Math.max(1,t)));return l.slice(0,n)}function T(e){let t="number"==typeof e?e:Number(e);return Number.isFinite(t)?Math.max(1,Math.min(4,Math.trunc(t))):1}function _(e){var t;let r=null==e||null==(t=e.visualAnalysis)?void 0:t.canvasRatio;if(!r)return null;let l=String(r).trim().toLowerCase();if(!l)return null;if(l.includes("16:9")||l.includes("landscape")||l.includes("wide"))return"16:9";if(l.includes("1:1")||l.includes("square"))return"1:1";if(l.includes("9:16")||l.includes("story")||l.includes("reel")||l.includes("vertical"))return"9:16";if(l.includes("4:5")||l.includes("feed portrait"))return"1:1";let n=l.match(/(\d+(?:\.\d+)?)\s*[:/x]\s*(\d+(?:\.\d+)?)/i);if(n){let e=Number(n[1]),t=Number(n[2]),r=t?e/t:0;return r>=1.45?"16:9":r<=.72?"9:16":"1:1"}return null}function x(e){var t,r;let l=(null!=(r=e.selectedProducts)?r:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()}).length||+!!e.productName;return l>1&&"per_product"===e.productLayout?Math.min(4,l):T(null!=(t=e.outputCount)?t:1)}function D(e){var t;let r=(null!=(t=e.selectedProducts)?t:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()});return r.length?r.slice(0,4):e.productName?[{name:e.productName,imageUrl:e.productImageUrl}]:[]}function L(e){try{let t=new URL(e).hostname.toLowerCase();if(/(^|\.)cdn\.klipio\.io$/i.test(t))return 0;if(/(^|\.)(cdn\.shopify\.com|shopifycdn\.net)$/i.test(t))return 1;if(/(^|\.)(fbcdn\.net|facebook\.com|fbsbx\.com|cdninstagram\.com)$/i.test(t))return 2;return 3}catch(e){return 9}}function B(e){var t,r,l,n;let a=new Set,o=[e.imageUrl,...null!=(t=e.imageCandidates)?t:[],null!=(r=e.thumbUrl)?r:void 0].filter(e=>!!e&&/^https:\/\//i.test(e)).filter(e=>
1!a.has(e)&&(a.add(e),!0)).sort((e,t)=>L(e)-L(t)).slice(0,6);return{imageUrl:null!=(l=o[0])?l:null,imageUrls:o,thumbUrl:null!=(n=e.thumbUrl)?n:null}}function H(e){var t,r;return!e.generating&&"done"!==e.status&&(!e.briefId||"failed"===e.status)&&(!e.ref||!!e.briefReady)&&D(e).length>0&&!!(e.analysis||(null==(t=e.ref)?void 0:t.hook)||(null==(r=e.customAngle)?void 0:r.trim()))}function K(e,t){var r,l;let n=null!=(r=t.claimedSlots)?r:[],a=null!=(l=t.skippedSlots)?l:[];return e&&(n.length||a.length)?{...e,productSlots:e.productSlots.map((e,t)=>n.includes(t)?{...e,role:"owned",confidence:1}:a.includes(t)?{...e,role:"foil",optional:!0,confidence:1,userSkipped:!0}:e)}:e}function E(e){return!e.generating&&(e.briefId||"done"===e.status||"failed"===e.status||e.build||e.builds)?{briefId:void 0,jobId:void 0,build:null,builds:void 0,assets:[],status:"idle",approved:!1,briefPayload:void 0,briefPayloads:void 0}:{}}function q(e){var t;return(null==(t=e.analysis)?void 0:t.headlineApproved)?{analysis:{...e.analysis,headlineApproved:!1}}:{}}function G(e){let{workspaceId:t,ads:r,products:a,quality:i,brandName:u,preferFreshAds:h,canvasId:y,serverLanes:S,serverLanesUpdatedAt:L,awaitingCanvas:G}=e,J=(0,l.useRef)(null!=y?y:null);J.current=null!=y?y:null;let F=(0,l.useRef)(S);F.current=S;let $=(0,l.useRef)(L);$.current=L;let[Y,Z]=(0,l.useState)([]),[V,W]=(0,l.useState)(!1),[Q,X]=(0,l.useState)(new Set),[ee,et]=(0,l.useState)(null),[er,el]=(0,l.useState)(null),en=(0,l.useRef)(!1),ea=(0,l.useRef)(e.canGenerate);ea.current=e.canGenerate;let eo=(0,l.useRef)(new Map),ei=(0,l.useRef)(null),eu=(0,l.useRef)(i);eu.current=i;let ec=(0,l.useRef)(Y);ec.current=Y;let es=(0,l.useRef)(t),ed=(0,l.useRef)(!1),ef=(0,l.useRef)(""),ep=(0,l.useRef)(new Map),ev=(0,l.useRef)(new Set),em=(0,l.useRef)(new Set),eg=(0,l.useCallback)((e,t)=>{Z(r=>r.map(r=>r.laneId===e?{...r,..."function"==typeof t?t(r):t}:r))},[]);(0,l.useEffect)(()=>{t&&es.current!==t&&(ei.current&&(clearTimeout(ei.current),ei.current=null),eo.current.clear(),en.current=!1,ed.current=!1,ef.current="",ep.current.clear(),ev.current.clear(),em.current.clear(),Z([]),es.current=t)},[t]),(0,l.useEffect)(()=>{if(!t||!h||0===r.length)return;let e=r.map(e=>e.adArchiveId).sort().join(",");if(e&&ef.current!==e){ef.current=e,ei.current&&(clearTimeout(ei.current),ei.current=null),eo.current.clear(),en.current=!1,ed.current=!1,ep.current.clear(),Z([]);try{window.localStorage.removeItem(p(t,J.current))}catch(e){}}},[t,h,r]),(0,l.useEffect)(()=>{var e,l;if(en.current||!t||G)return;let n=F.current,o=h&&r.length>0?{lanes:[],t:0,scoped:!1}:function(e,t){let r={lanes:[],t:0,scoped:!1};if(!e)return r;try{let l=window.localStorage.getItem(p(e,t)),n=l||(t?window.localStorage.getItem(p(e)):null);if(!n)return r;let a=JSON.parse(n);if((null==a?void 0:a.v)!==1||!Array.isArray(a.lanes)||"number"!=typeof a.t||Date.now()-a.t>6048e5)return r;return{lanes:C(a.lanes),t:a.t,scoped:!!l||!t}}catch(e){return r}}(t,J.current);if(n&&n.length){let t=Date.parse(null!=(e=$.current)?e:""),r=o.scoped&&o.lanes.length>0&&Number.isFinite(t)&&o.t>t+5e3;en.current=!0,Z(r?o.lanes:C(n)),r&&console.warn("[create-canvas] restored NEWER local copy over stale server canvas",{localT:o.t,serverAt:t});return}if(o.lanes.length){en.current=!0,Z(o.lanes);return}if(0===r.length)return;en.current=!0;let i=a[0];Z(r.map(e=>A(e,i)));let u="klipio:analysis:".concat(t,":").concat(r.map(e=>e.adArchiveId).sort().join(",")),c=e=>{let t=new Map(e.map(e=>[e.adArchiveId,e]));Z(e=>e.map(e=>{var r;if(!e.ref)return e;let l=null!=(r=t.get(e.ref.adArchiveId))?r:e.analysis,n=_(l);return{...e,analysis:l,aspectRatio:n&&(!e.aspectRatio||"1:1"===e.aspectRatio)?n:e.aspectRatio}}))};try{let e=sessionStorage.getItem(u);if(e){let t=JSON.parse(e);if(!(null==t||null==(l=t.a)?void 0:l.length)||t.a.some(e=>{var t;return(null!=(t=e.v)?t:0)<4||!e.composition}))sessionStorage.removeItem(u);else if(Array.isArray(t.a)&&"number"==typeof t.t&&Date.now()-t.t<6e5)return void c(t.a)}}catch(e){}W(!0),(async()=>{try{var e;let l=await fetch("/api/app2/create/preflight",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workspaceId:t,ads:r.map(e=>({adArchiveId:e.adArchiveId,...B(e),hook:e.hook,cta:e.cta,brand:e.competitorName})),products:a.slice(0,4).map(e=>({name:e.name}))})}),n=await l.json().catch(()=>({})),o=Array.isArray(n.dropped)?n.dropped:[];o.length&&(Z(e=>e.filter(e=>!e.ref||!o.includes(e.ref.adArchiveId))),el({code:"unsafe_dropped",error:"".concat(o.length," ad").concat(1===o.length?" was":"s were"," removed â they looked unsafe to re
1create.")}));let i=null!=(e=n.analyses)?e:[];if(c(i),i.length)try{sessionStorage.setItem(u,JSON.stringify({t:Date.now(),a:i}))}catch(e){}}catch(e){}finally{W(!1)}})()},[t,r,G]),(0,l.useEffect)(()=>{if(t&&en.current&&0!==Y.length)for(let l of Y){var e,r;let n=l.briefId||(l.builds?Object.values(l.builds).map(e=>e.briefId).find(Boolean):void 0);if(!n||em.current.has(n)||eo.current.has("".concat(l.laneId,"::").concat(n))||(null!=(r=null==(e=l.assets)?void 0:e.length)?r:0)>0&&"done"===l.status)continue;em.current.add(n);let a=l.laneId;(async()=>{try{var e,r,l,o,i;let u=await fetch("/api/app2/create?workspaceId=".concat(encodeURIComponent(t),"&briefId=").concat(encodeURIComponent(n)),{cache:"no-store"}),c=await u.json().catch(()=>({}));if(!(null==c?void 0:c.ok))return void em.current.delete(n);let s=Array.isArray(c.assets)?c.assets:[],d=v(null==(e=c.build)?void 0:e.status)||s.length>0;if(0===s.length&&!d)return void em.current.delete(n);eg(a,{briefId:n,build:null!=(o=c.build)?o:null,assets:s,status:s.length>0?"done":"failed",generating:!1,approved:(null==(r=c.brief)?void 0:r.status)==="approved",briefPayload:null!=(i=null==(l=c.brief)?void 0:l.payload)?i:void 0})}catch(e){em.current.delete(n)}})()}},[t,Y,eg]),(0,l.useEffect)(()=>{var e;if(!t||!en.current||V||0===Y.length)return;let r=Y.filter(e=>{var t;return e.ref&&!e.analysis&&(null!=(t=ep.current.get(e.laneId))?t:0)<2&&!Q.has(e.laneId)}).slice(0,10);if(0!==r.length){for(let t of r)ep.current.set(t.laneId,(null!=(e=ep.current.get(t.laneId))?e:0)+1);X(e=>{let t=new Set(e);for(let e of r)t.add(e.laneId);return t}),(async()=>{try{var e;let l=await fetch("/api/app2/create/preflight",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workspaceId:t,ads:r.map(e=>({adArchiveId:e.ref.adArchiveId,...B(e.ref),hook:e.ref.hook,cta:e.ref.cta,brand:e.ref.competitorName})),products:a.slice(0,4).map(e=>({name:e.name}))})}),n=await l.json().catch(()=>({})),o=Array.isArray(n.dropped)?n.dropped:[];o.length&&(Z(e=>e.filter(e=>!e.ref||!o.includes(e.ref.adArchiveId))),el({code:"unsafe_dropped",error:"".concat(o.length," ad").concat(1===o.length?" was":"s were"," removed â they looked unsafe to recreate.")}));let i=new Map((null!=(e=n.analyses)?e:[]).map(e=>[e.adArchiveId,e]));i.size&&Z(e=>e.map(e=>{if(!e.ref)return e;let t=i.get(e.ref.adArchiveId);if(!t)return e;let r=_(t);return{...e,analysis:t,aspectRatio:r&&(!e.aspectRatio||"1:1"===e.aspectRatio)?r:e.aspectRatio}}))}catch(e){}finally{X(e=>{let t=new Set(e);for(let e of r)t.delete(e.laneId);return t})}})()}},[t,Y,V,Q,a]);let eh=(0,l.useCallback)(async e=>{let r=ec.current.find(t=>t.laneId===e);if(!(!(null==r?void 0:r.ref)||Q.has(e))){ep.current.set(e,0),X(t=>{let r=new Set(t);return r.add(e),r});try{var l;let n=await fetch("/api/app2/create/preflight",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workspaceId:t,ads:[{adArchiveId:r.ref.adArchiveId,...B(r.ref),hook:r.ref.hook,cta:r.ref.cta,brand:r.ref.competitorName}],products:a.slice(0,4).map(e=>({name:e.name}))})}),o=(null!=(l=(await n.json().catch(()=>({}))).analyses)?l:[]).find(e=>e.adArchiveId===r.ref.adArchiveId);o&&eg(e,e=>{let t=_(o);return{analysis:o,aspectRatio:t&&(!e.aspectRatio||"1:1"===e.aspectRatio)?t:e.aspectRatio}})}catch(e){}finally{X(t=>{let r=new Set(t);return r.delete(e),r})}}},[t,a,Q,eg]);(0,l.useEffect)(()=>{ed.current||0===a.length||(ed.current=!0,Z(e=>e.map(e=>{var t,r,l,n,o,i,u;if(e.productName)return e;let f=P(a[0]);return{...e,productName:a[0].name,productImageUrl:f,productPrice:null!=(t=a[0].price)?t:null,selectedProducts:[{name:a[0].name,imageUrl:f}],laneProducts:e.laneProducts&&e.laneProducts.length?e.laneProducts:R(a[0]),productLayout:null!=(r=e.productLayout)?r:"together",productModelMode:null!=(l=e.productModelMode)?l:c,outputMode:null!=(n=e.outputMode)?n:e.ref?s:d,aspectRatio:null!=(o=e.aspectRatio)?o:"1:1",sizes:e.sizes&&e.sizes.length?e.sizes:[null!=(i=e.aspectRatio)?i:"1:1"],outputCount:null!=(u=e.outputCount)?u:1}})))},[a]);let[eb,ey]=(0,l.useState)("idle"),[ek,eI]=(0,l.useState)(!1),ew=(0,l.useRef)("");ew.current||(ew.current="t-".concat(Math.random().toString(36).slice(2,10)));let eC=(0,l.useRef)(!0),eA=(0,l.useRef)(0),eP=(0,l.useRef)(!1),eM=(0,l.useRef)(null),eS=(0,l.useRef)(null),eR=(0,l.useRef)(null),ez=(0,l.useRef)(null),eU=(0,l.useRef)(0);(0,l.useEffect)(()=>
1{if("undefined"==typeof BroadcastChannel)return;let e=J.current;if(!e)return;let t=null;try{t=new BroadcastChannel("klipio-canvas-".concat(e))}catch(e){return}return eM.current=t,t.onmessage=e=>{let t=e.data;if(!t||t.tabId===ew.current)return;let r=Date.now()-eA.current>4e3;if("claim"===t.type)r&&(eC.current=!1,eI(!0));else if("saved"===t.type&&Array.isArray(t.lanes)&&!eC.current&&r)try{eP.current=!0,Z(C(t.lanes)),ey("saved")}catch(e){}},()=>{try{null==t||t.close()}catch(e){}eM.current===t&&(eM.current=null)}},[y]);let eN=(0,l.useRef)(null),ej=(0,l.useRef)(null);(0,l.useEffect)(()=>{if(!t||!en.current||0===Y.length)return;eA.current=Date.now(),eU.current=0;let e=()=>{let e=Y.slice(0,12).map(m);try{let r={v:1,t:Date.now(),lanes:e};window.localStorage.setItem(p(t,J.current),JSON.stringify(r))}catch(r){try{let r={v:1,t:Date.now(),lanes:e.map(g)};window.localStorage.setItem(p(t,J.current),JSON.stringify(r)),console.warn("[create-canvas] localStorage quota â saved a minimal snapshot (outputs rehydrate on reopen)")}catch(e){console.warn("[create-canvas] localStorage blocked â offline cache unavailable")}}let r=J.current;if(!r)return;if(eP.current){eP.current=!1;return}eC.current=!0,eI(!1);try{var l;null==(l=eM.current)||l.postMessage({type:"claim",tabId:ew.current})}catch(e){}let n=JSON.stringify({id:r,lanes:e}),a=n.length>38e4?JSON.stringify({id:r,lanes:e.map(g)}):n;ey("saving"),fetch("/api/app2/canvas",{method:"PATCH",headers:{"Content-Type":"application/json"},body:a}).then(t=>{if(!t.ok)return void ey("error");ey("saved");try{var r;null==(r=eM.current)||r.postMessage({type:"saved",tabId:ew.current,lanes:e})}catch(e){}eS.current&&clearTimeout(eS.current),eS.current=setTimeout(()=>ey("idle"),2500)}).catch(()=>{ey("error")})};ej.current=e,eR.current=e,eN.current&&clearTimeout(eN.current),eN.current=setTimeout(()=>{eN.current=null,ej.current=null,e()},1e3)},[t,Y,y]),(0,l.useEffect)(()=>{if("saved"===eb||"idle"===eb){eU.current=0,ez.current&&(clearTimeout(ez.current),ez.current=null);return}"error"!==eb||eU.current>=5||ez.current||!eR.current||(ez.current=setTimeout(()=>{var e;ez.current=null,eU.current+=1,null==(e=eR.current)||e.call(eR)},Math.min(3e4,3e3*2**eU.current)))},[eb]),(0,l.useEffect)(()=>()=>{var e;ei.current&&(clearTimeout(ei.current),ei.current=null),eo.current.clear(),eN.current&&(clearTimeout(eN.current),eN.current=null),null==(e=ej.current)||e.call(ej),ej.current=null,eS.current&&(clearTimeout(eS.current),eS.current=null),ez.current&&(clearTimeout(ez.current),ez.current=null)},[]);let eO=(0,l.useCallback)((e,t)=>{let r=void 0!==t.source?t:a.find(e=>e.name===t.name),l=P(r);eg(e,e=>{var n,a;return{...E(e),...q(e),productName:t.name,productImageUrl:l,productPrice:null!=(n=null==r?void 0:r.price)?n:null,selectedProducts:[{name:t.name,imageUrl:l}],laneProducts:[{name:t.name,price:null!=(a=null==r?void 0:r.price)?a:null,images:M(r),role:"hero"}],productLayout:"together",outputCount:1}})},[a,eg]),eT=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r,l,n,o,i;let u=P(t),c=D(e),s=t.name.toLowerCase(),d=c.some(e=>e.name.toLowerCase()===s)?c.filter(e=>e.name.toLowerCase()!==s):[...c,{name:t.name,imageUrl:u}].slice(0,4),f=d.length?d:[{name:t.name,imageUrl:u}],p=a.find(e=>e.name===f[0].name),v=null!=(r=e.laneProducts)?r:[],m=f.map((e,t)=>{var r,l,n;let o=v.find(t=>t.name.toLowerCase()===e.name.toLowerCase()),i=a.find(t=>t.name===e.name);return{name:e.name,price:null!=(n=null!=(l=0===t?null==p?void 0:p.price:null==i?void 0:i.price)?l:null==o?void 0:o.price)?n:null,images:(null==o||null==(r=o.images)?void 0:r.length)?o.images:M(i),role:0===t?"hero":"supporting"}});return{...E(e),...q(e),selectedProducts:f,laneProducts:m,productName:f[0].name,productImageUrl:null!=(l=f[0].imageUrl)?l:void 0,productPrice:null!=(n=null==p?void 0:p.price)?n:null,productLayout:f.length>1&&null!=(o=e.productLayout)?o:"together",outputCount:T(null!=(i=e.outputCount)?i:1)}})},[a,eg]),e_=(0,l.useCallback)((e,t)=>{eg(e,e=>{let r=D(e).length,l=r>1?t:"together";return{...E(e),productLayout:l,outputCount:"per_product"===l?Math.min(4,r):1}})},[eg]),ex=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),productModelMode:t}))},[eg]),eD=(0,l.useCallback)((e,t)=>{let r=t.filter(e=>"female"===e||"male"===e).slice(0,4);eg(e,e=>({...E(e),modelGenders:r.length?r:void 0}))},[eg]),eL=(e,t)=>{if(!e.builds)return{};let r={};for(let[l,n]of Object.entries(e.builds))N(l).size!==t&&(r[l]=n);let l={...e,builds:Object.keys(r).length?r:void 0};return{builds:l.builds,...j(l)}},eB=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{var l;let n=e.ref?t:d;return r?{outputModeBySize:{...null!=(l=e.outputModeBySize)?l:{},[r]:n},...eL(e,r)}:{...E(e),outputMode:n}})},[eg]),eH=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{var l,n,a,o,i;let u=null!=(o=null==(l=e.outputKindsBySize)?void 0:l[t])?o:{creative:(null==(n=e.outputKinds)?void 0:n.creative)!==!1,plain:(null==(a=e.outputKinds)?void 0:a.plain)===!0},c={...u,[r]:!u[r]};return c.creative||c.plain?{outputKindsBySize:{...null!=(i=e.outputKindsBySize)?i:{},[t]:c},...eL(e,t)}:{}})},[eg]),eK=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),aspectRatio:t}))},[eg]),eE=(0,l.useCallback)((e,t)=>{eg(e,e=>{let r=T(t);return{...E(e),outputCount:r,sizes:O(e.sizes,r)}})},[eg]),eq=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;let l=e.sizes&&e.sizes.length?e.sizes:[null!=(r=e.aspectRatio)?r:"1:1"],n=l.includes(t)?l.filter(e=>e!==t):[...l,t],a=Math.max(1,x(e)),o=O(n.length?n:l,a);return{...E(e),sizes:o,aspectRatio:o[0]}})},[eg]),eG=(0,l.useCallback)((e,t)=>{var r;let l=ec.current.find(t=>t.laneId===e);if(0!==((null==l?void 0:l.sizes)&&l.sizes.length?l.sizes:[null!=(r=null==l?void 0:l.aspectRatio)?r:"1:1"]).filter(e=>e!==t).length){for(let[r,l]of eo.current)l.laneId===e&&N(l.size).size===t&&eo.current.delete(r);eg(e,e=>{var r,l,n;let a=(e.sizes&&e.sizes.length?e.sizes:[null!=(r=e.aspectRatio)?r:"1:1"]).filter(e=>e!==t);if(!a.length)return{};let o={...null!=(l=e.builds)?l:{}};for(let e of Object.keys(o))N(e).size===t&&delete o[e];let i=a.includes(null!=(n=e.aspectRatio)?n:"1:1")?e.aspectRatio:a[0];return{sizes:a,aspectRatio:i,builds:o}})}},[eg]),eJ=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{var l,n,a,o,i,u,c;let s=[...null!=(a=e.laneProducts)?a:[]],d={name:r.name,price:null!=(o=r.price)?o:null,images:(null!=(i=r.images)?i:[]).filter(e=>"string"==typeof e),role:0!==t||s.some((e,r)=>r!==t&&"hero"===e.role)?null!=(u=null==(l=s[t])?void 0:l.role)?u:"supporting":"hero",slotDescriptor:null!=(c=r.slotDescriptor)?c:null==(n=s[t])?void 0:n.slotDescriptor};return t>=s.length?s.push(d):s[t]=d,{...E(e),...z(e,s)}})},[eg]),eF=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;let l=[...null!=(r=e.laneProducts)?r:[]];return l.length>=o?{}:(l.push({name:"",price:null,images:[],role:0===l.length?"hero":"supporting",slotDescriptor:t}),{laneProducts:l})})},[eg]),e$=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;let l=(null!=(r=e.laneProducts)?r:[]).filter((e,r)=>r!==t);return{...E(e),...z(e,l)}})},[eg]),eY=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;let l=(null!=(r=e.laneProducts)?r:[]).map((e,r)=>({...e,role:r===t?"hero":"supporting"}));return{...E(e),...z(e,l)}})},[eg]),eZ=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),includeLogo:t}))},[eg]),eV=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),includePrice:t}))},[eg]),eW=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),direction:t.slice(0,500)}))},[eg]),eQ=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{var l;let n={...null!=(l=e.cdAnswers)?l:{}},a=r.slice(0,300);return a.trim()?n[t]=a:delete n[t],{...E(e),briefPayload:e.briefPayload,briefPayloads:e.briefPayloads,cdAnswers:n}})},[eg]),eX=(0,l.useCallback)((e,t)=>{eg(e,e=>{let r=Array.isArray(e.annotations)?e.annotations:[],l="undefined"!=typeof crypto&&"randomUUID"in crypto?crypto.randomUUID():"an_".concat(r.length,"_").concat(Math.round(performance.now()));return{annotations:[...r,{...t,id:l,status:"pending",createdAt:0}].slice(-24)}})},[eg]),e0=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;return{annotations:(null!=(r=e.annotations)?r:[]).filter(e=>e.id!==t)}})},[eg]),e1=(0,l.useCallback)((e,t)=>{eg(e,()=>({annotateTarget:null!=t?t:void 0}))},[eg]),e2=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;return(null==(r=e.annotations)?void 0:r.length)?{annotations:e.annotations.map(e=>"output"===e.target&&e.bucketKey===t&&"pending"===e.status?{...e,status:"applied"}:e)}:{}})},[eg]),e4=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),reviewText:t.slice(0,280)}
1))},[eg]),e5=(0,l.useCallback)((e,t)=>{let r=Math.max(0,Math.min(5,Math.round(Number.isFinite(t)?t:0)));eg(e,e=>({...E(e),starCount:r}))},[eg]),e3=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),reviewerName:t.slice(0,40)}))},[eg]),e6=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),cutoutUrl:t.trim()||void 0}))},[eg]),e9=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{let l=t.trim()||void 0;return e.typographyAssetId===l?{}:{...E(e),typographyAssetId:l,typographyLabel:l&&(null==r?void 0:r.slice(0,60))||void 0}})},[eg]),e8=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{let l=t.trim()||void 0;return e.backgroundAssetId===l?{}:{...E(e),backgroundAssetId:l,backgroundThumb:l&&(null==r?void 0:r.trim())||void 0}})},[eg]),e7=(0,l.useCallback)((e,t,r)=>{eg(e,e=>{let l=t.trim()||void 0;if(e.modelAssetId===l)return{};let n=(e.direction||"").trim(),a=(r||"").slice(0,200),o=!a||n.includes(a)?n||void 0:(n?"".concat(n," \xb7 ").concat(a):a).slice(0,500);return{...E(e),modelAssetId:l,direction:o}})},[eg]),te=(0,l.useCallback)((e,t)=>{t<=0||eg(e,e=>{var r;let l=null!=(r=e.skippedSlots)?r:[],n=l.includes(t)?l.filter(e=>e!==t):[...l,t];return{...E(e),skippedSlots:n}})},[eg]),tt=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r;let l=null!=(r=e.claimedSlots)?r:[];return l.includes(t)?e:{...E(e),claimedSlots:[...l,t]}})},[eg]),tr=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r,l;let n=[...null!=(r=e.laneProducts)?r:[]];if(t>=0&&t<n.length){let e=n[t];n[t]={name:"",price:null,images:[],role:"supporting",slotDescriptor:null==e?void 0:e.slotDescriptor}}let a=(null!=(l=e.claimedSlots)?l:[]).filter(e=>e!==t);return{...E(e),...z(e,n),claimedSlots:a}})},[eg]),tl=(0,l.useCallback)((e,t)=>{Z(r=>r.map(r=>{var l,n,a,o,i,u,c,s;if(r.laneId!==e)return r;let d="Product-led creative",f=null!=(c=r.analysis)?c:{adArchiveId:null!=(o=null==(l=r.ref)?void 0:l.adArchiveId)?o:"",sourceHeadline:null!=(i=null==(n=r.ref)?void 0:n.hook)?i:"",correctedHeadline:d,correctedOffer:"none",angle:d,styleDescriptors:"",whyChips:[],diff:{from:null!=(u=null==(a=r.ref)?void 0:a.hook)?u:"",to:d},lengthMatched:!0},p="correctedHeadline"in t&&"string"==typeof t.correctedHeadline&&t.correctedHeadline.trim()!==(null!=(s=f.correctedHeadline)?s:"").trim();return{...r,...E(r),analysis:{...f,...t,...p?{headlineApproved:!0}:{}}}}))},[]),tn=(0,l.useCallback)((e,t,r,l)=>{Z(a=>a.map(a=>{var o,i,u,c,s,d,f,p,v,m,g;if(a.laneId!==e)return a;let h=(null==(o=a.sizes)?void 0:o[0])||"4:5",b=null!=l?l:h,y=null==(i=a.briefPayloads)?void 0:i[b],k=null!=y?y:b===h?a.briefPayload:void 0,I=null==k?void 0:k.blockMap,w=null==I?void 0:I[t];if(!k||!I||!w)return a;let C=r.slice(0,120),A=I.map((e,r)=>r===t?{...e,text:C,chars:C.length}:e),P={...y?{briefPayloads:{...a.briefPayloads,[b]:{...y,blockMap:A}}}:{},...b===h&&a.briefPayload?{briefPayload:{...a.briefPayload,blockMap:A}}:{}},M=K(null==(u=a.analysis)?void 0:u.composition,a);if(b===h&&"hook"===w.role&&(0,n._z)(A,M)===A[t]){let e="Product-led creative",t=null!=(m=a.analysis)?m:{adArchiveId:null!=(f=null==(c=a.ref)?void 0:c.adArchiveId)?f:"",sourceHeadline:null!=(p=null==(s=a.ref)?void 0:s.hook)?p:"",correctedHeadline:e,correctedOffer:"none",angle:e,styleDescriptors:"",whyChips:[],diff:{from:null!=(v=null==(d=a.ref)?void 0:d.hook)?v:"",to:e},lengthMatched:!0},r=C.trim()!==(null!=(g=t.correctedHeadline)?g:"").trim();P.analysis={...t,correctedHeadline:C,...r?{headlineApproved:!0}:{}}}return{...a,...P}}))},[]),ta=(0,l.useCallback)((e,t,r)=>{Z(l=>l.map(l=>{var n,a;if(l.laneId!==e)return l;let o=(null==(n=l.sizes)?void 0:n[0])||"4:5",i=null!=r?r:o,u=null==(a=l.briefPayloads)?void 0:a[i],c=null!=u?u:l.briefPayload,s=null==c?void 0:c.blockMap,d=null==s?void 0:s[t];if(!c||!s||!d)return l;let f=!1===d.enabled,p=s.map((e,r)=>r===t?{...e,enabled:f}:e),v=i===o?{...u?{briefPayloads:{...l.briefPayloads,[i]:{...u,blockMap:p}}}:{},...l.briefPayload?{briefPayload:{...l.briefPayload,blockMap:p}}:{}}:{briefPayloads:{...l.briefPayloads,[i]:{...c,blockMap:p}}};return{...l,...v}}))},[]),to=(0,l.useCallback)(async e=>{var r,l,n,a,o,i,f,p,v,m,g,h,y,k,I,w,C,A;let P=ec.current.find(t=>t.laneId===e);if(!P||P.briefGenerating)return{ok:!1};let M=(null!=(n=P.laneProducts)?n:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()}),S=null!=(a=M.find(e=>"hero"===e.role))?a:M[0],R=S?{name:S.name,price:null!=(o=S.price)?o:void 0}:(null==(l=P.selectedProducts)||null==(r=l[0])?void 0:r.name)?{name:P.selectedProducts[0].name,price:null!=(i=P.productPrice)?i:void 0}:null;if(!(null==R?void 0:R.name))return{ok:!1,error:"Pick a product first."};let z=b(P);eg(e,{briefGenerating:!0});try{let r=P.analysis,l=K(null==r?void 0:r.composition,P),n=[P.cutoutUrl||(null==S||null==(f=S.images)?void 0:f[0])||(null==(v=P.selectedProducts)||null==(p=v[0])?void 0:p.imageUrl)].filter(
1Boolean),a=[P.cutoutUrl||void 0,...null!=(y=null==S?void 0:S.images)?y:[],null==(g=P.selectedProducts)||null==(m=g[0])?void 0:m.imageUrl].filter(e=>"string"==typeof e&&!!e).filter((e,t,r)=>r.indexOf(e)===t).slice(0,3),o=null!=(k=null==(h=P.briefPayload)?void 0:h.questions)?k:[],i=Object.entries(null!=(I=P.cdAnswers)?I:{}).filter(e=>{let[,t]=e;return"string"==typeof t&&t.trim()}).map(e=>{var t,r;let[l,n]=e;return{question:null!=(r=null==(t=o.find(e=>e.code===l))?void 0:t.question)?r:l,answer:n.trim().slice(0,300)}}).slice(0,6),b=(P.sizes&&P.sizes.length?P.sizes:["4:5"]).slice(0,3),U=await Promise.all(b.map(async e=>{try{var o,f,p,v,m,g,h,b,y,k;let I=await fetch("/api/app2/create/brief-preview",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workspaceId:t,product:R,supporting:M.filter(e=>e!==S).slice(0,3).map(e=>({name:e.name})),brandName:u||void 0,customAngle:P.customAngle||void 0,analysis:r?{sourceHeadline:r.sourceHeadline,headlineCase:null==(o=r.visualAnalysis)?void 0:o.headlineCase,angle:r.angle,angleName:null==(f=r.creativeAngle)?void 0:f.name,correctedOffer:r.correctedOffer}:void 0,referenceImageUrl:(null==(p=P.ref)?void 0:p.imageUrl)||void 0,referenceImageUrls:(null==(v=P.ref)?void 0:v.imageCandidates)&&P.ref.imageCandidates.length?P.ref.imageCandidates:void 0,productImageUrls:n.length?n:void 0,productViews:a.length>1?a:void 0,cdAnswers:i.length?i:void 0,composition:l,heroProduct:R,includePrice:null!=(h=P.includePrice)?h:!l||(null==(m=l.price)?void 0:m.present)===!0,includeLogo:null!=(b=P.includeLogo)?b:!l||(null==(g=l.logo)?void 0:g.present)!==!1,aspectRatio:e,productModelMode:null!=(y=P.productModelMode)?y:c,outputMode:null!=(k=P.outputMode)?k:P.ref?s:d,correctedHeadline:(null==r?void 0:r.correctedHeadline)||void 0,styleDescriptors:(null==r?void 0:r.styleDescriptors)||void 0,creativeAngle:(null==r?void 0:r.creativeAngle)||void 0})}),w=await I.json().catch(()=>({}));return{sz:e,ok:I.ok&&!!w.ok&&!!w.headline,json:w}}catch(t){return{sz:e,ok:!1,json:{}}}})),N=null!=(w=U.find(e=>e.sz===b[0]&&e.ok))?w:U.find(e=>e.ok);if(!N)return eg(e,{briefGenerating:!1}),{ok:!1,error:(null==(A=U[0])||null==(C=A.json)?void 0:C.error)||"Couldn't write the brief â try again."};let j=N.json,O=j.headline;return Z(t=>t.map(t=>{var r,l,n,a,o,i,u,c;if(t.laneId!==e)return t;let s="Product-led creative",d=null!=(u=t.analysis)?u:{adArchiveId:null!=(a=null==(r=t.ref)?void 0:r.adArchiveId)?a:"",sourceHeadline:null!=(o=null==(l=t.ref)?void 0:l.hook)?o:"",correctedHeadline:s,correctedOffer:"none",angle:s,styleDescriptors:"",whyChips:[],diff:{from:null!=(i=null==(n=t.ref)?void 0:n.hook)?i:"",to:s},lengthMatched:!0},f=e=>{let t=e.json,r=t.headline||O;if(t.blockMap||t.concerns||t.questions)return{angle:r,why:[],hookLines:[r],imageConcepts:t.concept?[t.concept]:[],blockMap:t.blockMap,concerns:t.concerns,questions:t.questions,forAspect:e.sz}},p={};for(let e of U){if(!e.ok)continue;let t=f(e);t&&(p[e.sz]=t)}let v=p[N.sz],m=!!v,g=m?{...null!=(c=t.briefPayload)?c:{},...v,questions:null==v?void 0:v.questions}:void 0;return{...t,...E(t),analysis:{...d,correctedHeadline:O,headlineApproved:!1,diff:{...d.diff,to:O},lengthMatched:O.split(/\s+/).length<=8},briefReady:!0,briefGenerating:!1,briefConcept:j.concept||void 0,briefForKey:z,...m?{briefPayload:g}:{},...Object.keys(p).length?{briefPayloads:p}:{}}})),{ok:!0}}catch(t){return eg(e,{briefGenerating:!1}),{ok:!1,error:"Network error â try again."}}},[t,u,eg]),ti=(0,l.useCallback)((e,t)=>{eg(e,e=>{var r,l;let n={creative:(null==(r=e.outputKinds)?void 0:r.creative)!==!1,plain:(null==(l=e.outputKinds)?void 0:l.plain)===!0},a={...n,[t]:!n[t]};return a.creative||a.plain?{...E(e),outputKinds:a}:{}})},[eg]),tu=(0,l.useCallback)((e,t)=>{eg(e,e=>({...E(e),customAngle:t}))},[eg]),tc=(0,l.useCallback)(()=>{let e=P(a[0]);Z(t=>{var r,l,n;return[...t,{laneId:f(),ref:null,customAngle:"",productName:(null==(r=a[0])?void 0:r.name)||"",productImageUrl:e,productPrice:null!=(n=null==(l=a[0])?void 0:l.price)?n:null,selectedProducts:a[0]?[{name:a[0].name,imageUrl:e}]:[],laneProducts:R(a[0]),productLayout:"together",productModelMode:c,outputMode:d,aspectRatio:"1:1",sizes:["1:1"],outputCount:1,analysis:null,build:null,assets:[],generating:!1,status:"idle"}]})},[a]),ts=(0,l.useCallback)(e=>{el(null);let r=ec.current;if(r.length>=12)return void el({code:"lane_cap",error:"You can work on up to ".concat(12," ads at once. Remove one to add another.")});let l="lane-".concat(e.adArchiveId);if(r.some(e=>e.laneId===l))return;Z(t=>[...t,{...A(e,a[0]),laneId:l}]);let n="klipio:analysis1:".concat(t,":").concat(e.adArchiveId),o=e=>eg(l,{analysis:e});try{let e=sessionStorage.getItem(n);if(e){let t=JSON.parse(e);if((null==t?void 0:t.a)&&"number"==typeof t.t&&Date.now()-t.t<6e5)return void o(t.a)}}catch(e){}X(e=>
1new Set(e).add(l)),(async()=>{try{var r;let i=await fetch("/api/app2/create/preflight",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workspaceId:t,ads:[{adArchiveId:e.adArchiveId,...B(e),hook:e.hook,cta:e.cta,brand:e.competitorName}],products:a.slice(0,4).map(e=>({name:e.name}))})}),u=await i.json().catch(()=>({}));if(Array.isArray(u.dropped)&&u.dropped.includes(e.adArchiveId)){Z(e=>e.filter(e=>e.laneId!==l)),el({code:"unsafe_dropped",error:"That ad looked unsafe to recreate â it wasn't added."});return}let c=null==(r=u.analyses)?void 0:r[0];if(c){o(c);try{sessionStorage.setItem(n,JSON.stringify({t:Date.now(),a:c}))}catch(e){}}}catch(e){}finally{X(e=>{let t=new Set(e);return t.delete(l),t})}})()},[t,a,eg]),td=(0,l.useCallback)(e=>{if(el(null),ec.current.length>=12)return void el({code:"lane_cap",error:"You can work on up to ".concat(12," ads at once.")});let t=P(e);Z(r=>{var l;return[...r,{laneId:f(),ref:null,customAngle:"",productName:e.name,productImageUrl:t,productPrice:null!=(l=e.price)?l:null,selectedProducts:[{name:e.name,imageUrl:t}],laneProducts:R(e),productLayout:"together",productModelMode:c,outputMode:d,aspectRatio:"1:1",sizes:["1:1"],outputCount:1,analysis:null,build:null,assets:[],generating:!1,status:"idle"}]})},[]),tf=(0,l.useCallback)(e=>{for(let[t,r]of eo.current)r.laneId===e&&eo.current.delete(t);Z(t=>t.filter(t=>t.laneId!==e))},[]),tp=(0,l.useCallback)((e,t,r)=>{Z(l=>l.map(l=>{var n,a;if(l.laneId!==e)return l;let o=null!=(n=l.builds)?n:{},i=null!=(a=o[t])?a:{build:null,assets:[],status:"generating",generating:!0},u={...o,[t]:{...i,...r}},c={...l,builds:u};return{...c,...j(c)}}))},[]),tv=(0,l.useCallback)(async()=>{ei.current=null;let e=Array.from(eo.current.entries());await Promise.all(e.map(async e=>{let[r,l]=e;if(!ec.current.some(e=>e.laneId===l.laneId))return void eo.current.delete(r);l.polls+=1;try{var n,a,o,i,u,c,s,d,f,p,v,m,g,h,b,y,k,I;let e=await fetch("/api/app2/create?workspaceId=".concat(encodeURIComponent(t),"&briefId=").concat(encodeURIComponent(l.briefId)),{cache:"no-store"}),w=await e.json().catch(()=>({})),C=(null==(i=w.brief)||null==(o=i.payload)||null==(a=o.blockMap)||null==(n=a.find(e=>"hook"===e.role))?void 0:n.text)||(null==(s=w.brief)||null==(c=s.payload)||null==(u=c.hookLines)?void 0:u[0]),A=null!=(m=null==(d=w.build)?void 0:d.status)?m:null,P="completed"===A||"failed"===A||"dead_letter"===A,M=!P&&l.polls>=120,S=M?{...null!=(g=w.build)?g:{status:"processing",reason:null},reason:null!=(h=null==(f=w.build)?void 0:f.reason)?h:"timeout"}:null!=(b=w.build)?b:null;tp(l.laneId,l.size,{briefId:l.briefId,build:S,jobId:null!=(y=null==(p=w.build)?void 0:p.id)?y:void 0,assets:null!=(k=w.assets)?k:[],generating:!P&&!M,status:P?(null!=(I=null==(v=w.assets)?void 0:v.length)?I:0)>0?"done":"failed":M?"failed":"generating",..."string"==typeof C&&C.trim()?{briefHeadline:C.trim().slice(0,80)}:{}}),(P||M)&&eo.current.delete(r)}catch(e){l.polls>=120&&(tp(l.laneId,l.size,{generating:!1,status:"failed",build:{status:"failed",reason:"network_timeout"}}),eo.current.delete(r))}})),eo.current.size>0&&!ei.current&&(ei.current=setTimeout(()=>{tm.current()},2500))},[t,tp]),tm=(0,l.useRef)(tv);(0,l.useEffect)(()=>{tm.current=tv},[tv]);let tg=(0,l.useCallback)((e,t,r)=>{eo.current.set("".concat(e,"::").concat(t),{laneId:e,briefId:t,size:r,polls:0}),ei.current||(ei.current=setTimeout(()=>{tm.current()},350))},[]);(0,l.useEffect)(()=>{if(t)for(let t of Y){var e,r,l;let n=t.builds?Object.entries(t.builds):[];if(n.length){for(let[e,l]of n)!(!l.briefId||eo.current.has("".concat(t.laneId,"::").concat(l.briefId)))&&("done"===l.status||v(null==(r=l.build)?void 0:r.status)||tg(t.laneId,l.briefId,e));continue}!(!t.briefId||eo.current.has("".concat(t.laneId,"::").concat(t.briefId)))&&("done"===t.status||v(null==(e=t.build)?void 0:e.status)||tg(t.laneId,t.briefId,null!=(l=t.aspectRatio)?l:"1:1"))}},[t,Y,tg]),(0,l.useEffect)(()=>{if(t)for(let l of Y){var e,r;let n=null!=(r=l.briefId)?r:l.builds?Object.values(l.builds).map(e=>e.briefId).find(Boolean):void 0;if(!n)continue;let a=v(null==(e=l.build)?void 0:e.status)||!!(l.builds&&Object.values(l.builds).some(e=>{var t;return(null==e?void 0:e.status)==="done"||v(null==e||null==(t=e.build)?void 0:t.status)})),o=!!l.briefPayload&&!l.briefPayload.packageText&&a&&!ev.current.has("".concat(n,":pkg"));if(l.briefPayload&&!o){ev.current.add(n);continue}if(!l.build&&!l.builds||ev.current.has(n)&&!o)continue;let i=o?"".concat(n,":pkg"):n;ev.current.add(i);let u=l.laneId;(async()=>{try{var e;let r=await fetch("/api/app2/create?workspaceId=".concat(encodeURIComponent(t),"&briefId=").concat(encodeURIComponent(n)),{
1cache:"no-store"}),l=null==(e=(await r.json().catch(()=>({}))).brief)?void 0:e.payload;l?eg(u,{briefPayload:l}):ev.current.delete(i)}catch(e){ev.current.delete(i)}})()}},[t,Y,eg]);let th=(0,l.useCallback)(e=>{var t,r,l,n,a,o,i,f,p,v,m,g,h,b,y,C,A,M,S,R,z,U,N,j,T,_,L,B,H,E,q,G;let F=e.analysis,$=null==F?void 0:F.creativeAngle,Y=$?[$.name,$.briefInstruction].filter(Boolean).join(" â "):"",Z=(null==F||null==(t=F.visualAnalysis)?void 0:t.composition)||(null==F?void 0:F.styleDescriptors)||"",V=(Y||Z||(null==F?void 0:F.angle)||(null==F?void 0:F.correctedHeadline)||e.customAngle||(e.ref?"Reference creative template":"")).trim();if(!V)return{ok:!1,code:"no_angle",error:"Add an idea or pick an ad first."};let W=D(e),Q=null!=(S=W[0])?S:function(e){if(!(null==e?void 0:e.name))return null;let t=void 0!==e.source?e:void 0;return{name:e.name,imageUrl:P(t)}}({name:e.productName});if(!(null==Q?void 0:Q.name))return{ok:!1,code:"no_product",error:"Assign a product to this lane first."};let X=W.length>1&&"per_product"===e.productLayout?"per_product":"together",ee="per_product"===X?Math.min(4,W.length):x(e),et="per_product"===X?W.slice(0,ee):W,er=k(e),el=Math.max(1,"per_product"===X?et.length:ee),en=Math.max(1,...(e.sizes&&e.sizes.length?e.sizes:[null!=(R=e.aspectRatio)?R:"4:5"]).map(t=>I(e,t).length)),ea=O(e.sizes,el*en),eo={},ei={};for(let t of ea)eo[t]=I(e,t),ei[t]=w(e,t);let eu=(null!=(z=e.laneProducts)?z:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()}),ec=function(e){var t,r,l,n,a;let o=(null!=(t=e.laneProducts)?t:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()});if(o.length)return null!=(r=o.find(e=>"hero"===e.role))?r:o[0];let i=(null!=(l=e.selectedProducts)?l:[]).filter(e=>{var t;return null==e||null==(t=e.name)?void 0:t.trim()});return i.length?{name:i[0].name,price:null!=(n=e.productPrice)?n:null,images:i[0].imageUrl?[i[0].imageUrl]:[],role:"hero"}:e.productName?{name:e.productName,price:null!=(a=e.productPrice)?a:null,images:e.productImageUrl?[e.productImageUrl]:[],role:"hero"}:null}(e),es=eu.filter(e=>"hero"!==e.role).slice(0,3),ed=null!=(j=null!=(N=null!=(U=(null==(r=e.cutoutUrl)?void 0:r.trim())||void 0)?U:null==ec||null==(l=ec.images)?void 0:l[0])?N:Q.imageUrl)?j:void 0,ef=K(null==F?void 0:F.composition,e),ep=null!=(T=e.includeLogo)?T:!ef||(null==(n=ef.logo)?void 0:n.present)!==!1,ev=null!=(_=e.includePrice)?_:!ef||(null==(a=ef.price)?void 0:a.present)===!0,em=(null==(o=e.direction)?void 0:o.trim())?e.direction.trim().slice(0,500):void 0,eg=(e,t)=>{var r,l;let n=[e,...null!=(l=null==(r=eu[t])?void 0:r.images)?l:[]].filter(e=>"string"==typeof e&&!!e).filter((e,t,r)=>r.indexOf(e)===t).slice(0,3);return n.length>1?n:void 0},eh=et.map((e,t)=>{var r,l,n,a,o;let i=null!=(o=null!=(a=null!=(n=0===t?ed:void 0)?n:null==(l=eu[t])||null==(r=l.images)?void 0:r[0])?a:e.imageUrl)?o:void 0;return{name:e.name,imageUrl:i,imageUrls:eg(i,t)}}),eb=null!=(L=null==(i=e.briefPayload)?void 0:i.questions)?L:[],ey=Object.entries(null!=(B=e.cdAnswers)?B:{}).filter(e=>{let[,t]=e;return"string"==typeof t&&t.trim()}).map(e=>{var t,r;let[l,n]=e;return{question:null!=(r=null==(t=eb.find(e=>e.code===l))?void 0:t.question)?r:l,answer:n.trim().slice(0,300)}}).slice(0,6);return{ok:!0,basePattern:{angle:V,brand:null==(f=e.ref)?void 0:f.competitorName,chips:null==F||null==(p=F.whyChips)?void 0:p.map(e=>e.text).filter(Boolean),offer:null==F?void 0:F.correctedOffer,correctedHeadline:null==F?void 0:F.correctedHeadline,headlineApproved:(null==F?void 0:F.headlineApproved)===!0,styleDescriptors:null==F?void 0:F.styleDescriptors,creativeAngle:null==F?void 0:F.creativeAngle,layoutDescriptor:null==F?void 0:F.layoutDescriptor,productModelMode:null!=(H=e.productModelMode)?H:c,modelGenders:e.modelGenders&&e.modelGenders.length?e.modelGenders:void 0,outputMode:null!=(E=e.outputMode)?E:e.ref?s:d,referenceImageUrl:null==(v=e.ref)?void 0:v.imageUrl,referenceImageUrls:null==(m=e.ref)?void 0:m.imageCandidates,referenceAdArchiveId:null==(g=e.ref)?void 0:g.adArchiveId,references:e.ref?[{hook:e.ref.hook,title:void 0}]:void 0,product:Q.name,productImageUrl:ed,productImageUrls:eg(ed,0),products:eh,layout:X,cdAnswers:ey.length?ey:void 0,userBrandName:u||void 0,canvasId:null!=(q=J.current)?q:void 0,laneId:e.laneId,heroProduct:ec?{name:ec.name,price:null!=(G=ec.price)?G:void 0}:void 0,supportingProducts:es.length?es.map(e=>{var t;return{name:e.name,price:null!=(t=e.price)?t:void 0}}):void 0,includeLogo:ep,includePrice:ev,direction:em,annotations:Array.isArray(e.annotations)&&e.annotations.length?e.annotations.filter(e=>"pending"===e.status).slice(0,24):void 0,composition:ef,blockMap:null==(h=e.briefPayload)?void 0:h.blockMap,reviewText:(null==(b=e.reviewText)?void 0:b.trim())||void 0,starCount:"number"==typeof e.starCount&&e.starCount>0?e.starCount:void 0,reviewerName:(null==(y=e.reviewerName)?void 0:y.trim())||void 0,typographyAssetId:(null==(C=e.typographyAssetId)?void 0:C.trim())||void 0,backgroundAssetId:(null==(A=e.backgroundAssetId)?void 0:A.trim())||void 0},layout:X,perCount:el,sizes:ea,kinds:er,kindsBySize:eo,modeBySize:ei,productEntries:eh,blockAspect:null==(M=e.briefPayload)?void 0:M.forAspect,briefBySize:e.briefPayloads}},[u]),tb=(0,l.useCallback)((e,t,r,l)=>{var n,a,o,i,u,c;let s=function(e,t,r){let l="".concat("ad","~").concat(t,"~").concat(r),n=0x811c9dc5;for(let e=0;e<l.length;e++)n^=l.charCodeAt(e),n=Math.imul(n,0x1000193);return Math.abs(n)%1e5}(0,r,l),d=null!=(i=null==(a=e.briefBySize)||null==(n=a[t])?void 0:n.blockMap)?i:e.blockAspect&&e.blockAspect!==t?void 0:e.basePattern.blockMap,f=null==d?void 0:d.filter(e=>!1!==e.enabled),p=f&&f.length?f:void 0,v=null!=(u=null==(o=e.modeBySize)?void 0:o[t])?u:e.basePattern.outputMode,m=U(t,r,l),g=e.basePattern.annotations,h=Array.isArray(g)?g.filter(e=>(null==e?void 0:e.target)!=="output"||(null==e?void 0:e.bucketKey)===m):void 0,b=h&&h.length?h:void 0;if("per_product"===e.layout&&e.productEntries[l]){let n=e.productEntries[l];return{...e.basePattern,annotations:b,blockMap:p,outputMode:v,aspectRatio:t,outputKind:r,variationSeed:s,layout:"together",products:[n],product:n.name,productImageUrl:null!=(c=n.imageUrl)?c:e.basePattern.productImageUrl}}return{...e.basePattern,annotations:b,blockMap:p,outputMode:v,aspectRatio:t,outputKind:r,variationSeed:s}},[]),ty=(0,l.useCallback)(async(e,r,l)=>{try{let a=await fetch("/api/app2/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workspaceId:t,quality:eu.current,pattern:l,outputCount:1})}),o=await a.json().catch(()=>({}));if(a.ok&&o.ok&&o.briefId){var n;return tp(e,r,{briefId:o.briefId,jobId:o.jobId,build:{status:"queued",reason:null,id:null!=(n=o.jobId)?n:null},assets:[],status:"generating",generating:!0}),tg(e,o.briefId,r),{ok:!0}}return tp(e,r,{generating:!1,status:"failed",build:{status:"failed",reason:o.code||"all_failed"}}
1),{ok:!1,code:o.code,error:o.error}}catch(t){return tp(e,r,{generating:!1,status:"failed",build:{status:"failed",reason:"network_timeout"}}),{ok:!1,code:"network",error:"Network error â try again."}}},[t,tp,tg]),tk=(0,l.useCallback)(async function(e){var t,r,l;let n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],a=ec.current.find(t=>t.laneId===e);if(!a)return{ok:!1};if(a.generating||!n&&("done"===a.status||a.briefId&&"failed"!==a.status))return{ok:!1,code:"busy"};if(!ea.current)return{ok:!1,code:"upgrade",error:"Generating the images unlocks on a paid plan."};!a.briefReady&&D(a).length>0&&(await to(e).catch(()=>null),a=null!=(r=ec.current.find(t=>t.laneId===e))?r:a);let o=th(a);if(!o.ok)return eg(e,{status:"idle"}),{ok:!1,code:o.code,error:o.error};let i=[];for(let e of o.sizes)for(let r of null!=(l=null==(t=o.kindsBySize)?void 0:t[e])?l:o.kinds)for(let t=0;t<o.perCount;t++)i.push({bucket:U(e,r,t),size:e,kind:r,index:t});eg(e,()=>{let e={};for(let t of i)e[t.bucket]={build:{status:"queued",reason:null},assets:[],status:"generating",generating:!0};return{generating:!0,status:"generating",build:{status:"queued",reason:null},assets:[],briefId:void 0,jobId:void 0,approved:!1,builds:e}});let u=null,c=!1;for(let t of i){let r=await ty(e,t.bucket,tb(o,t.size,t.kind,t.index));r.ok?c=!0:u||(u=r)}return c?{ok:!0}:(eg(e,()=>({generating:!1,status:"failed"})),null!=u?u:{ok:!1,code:"all_failed"})},[eg,to,th,tb,ty]),tI=(0,l.useCallback)(async function(e,t){var r,l,n,a,o;let i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],u=ec.current.find(t=>t.laneId===e);if(!u)return{ok:!1};if(!ea.current)return{ok:!1,code:"upgrade",error:"Generating the images unlocks on a paid plan."};!u.briefReady&&D(u).length>0&&(await to(e).catch(()=>null),u=null!=(l=ec.current.find(t=>t.laneId===e))?l:u);let c=th(u);if(!c.ok)return el({code:c.code,error:c.error||"Fix the lane inputs first."}),{ok:!1,code:c.code,error:c.error};if(!c.sizes.includes(t))return{ok:!1,code:"no_size"};let s=[];for(let e of null!=(n=null==(r=c.kindsBySize)?void 0:r[t])?n:c.kinds)for(let r=0;r<c.perCount;r++)s.push({bucket:U(t,e,r),kind:e,index:r});let d=null,f=!1;for(let r of s){let l=null==(o=ec.current.find(t=>t.laneId===e))||null==(a=o.builds)?void 0:a[r.bucket];if((null==l?void 0:l.generating)&&!i)continue;for(let[t,l]of eo.current)l.laneId===e&&l.size===r.bucket&&eo.current.delete(t);tp(e,r.bucket,{briefId:void 0,jobId:void 0,build:{status:"queued",reason:null},assets:[],status:"generating",generating:!0});let n=await ty(e,r.bucket,tb(c,t,r.kind,r.index));n.ok?f=!0:d||(d=n)}if(f)return{ok:!0};for(let t of s)tp(e,t.bucket,{generating:!1,status:"failed"});return(null==d?void 0:d.code)&&"busy"!==d.code&&el({code:d.code,error:d.error||"Couldn't generate this size."}),null!=d?d:{ok:!1,code:"all_failed"}},[to,th,tb,ty,tp]),tw=(0,l.useCallback)(async(e,t)=>{var r,l,n;let a=ec.current.find(t=>t.laneId===e);if(!a)return{ok:!1};let o=null==(r=a.builds)?void 0:r[t];if(null==o?void 0:o.generating)return{ok:!1};if(!ea.current)return el({code:"upgrade",error:"Generating the images unlocks on a paid plan."}),{ok:!1};let i=th(a);if(!i.ok)return el({code:i.code,error:i.error||"Fix the lane inputs first."}),{ok:!1};let{size:u,kind:c,index:s}=N(t);for(let[r,l]of eo.current)l.laneId===e&&l.size===t&&eo.current.delete(r);let d=null==o?void 0:o.assets,f=Array.isArray(d)&&d.length?[{assets:d,summary:function(e,t){let r=(null!=e?e:[]).filter(e=>"output"===e.target&&e.bucketKey===t&&"pending"===e.status);if(!r.length)return"Regenerated";let l=e=>"remove"===e||"ignore"===e?"Remove":"change"===e?"Change":"move"===e?"Move":"keep"===e?"Keep":"Note";return r.slice(0,4).map(e=>e.note?"".concat(l(e.intent),": ").concat(e.note.slice(0,40)):l(e.intent)).join("; ")+(r.length>4?" +".concat(r.length-4," more"):"")}(a.annotations,t),at:Date.now()},...null!=(l=null==o?void 0:o.versions)?l:[]].slice(0,8):null!=(n=null==o?void 0:o.versions)?n:[];tp(e,t,{briefId:void 0,jobId:void 0,build:{status:"queued",reason:null},assets:[],status:"generating",generating:!0,versions:f});let p=await ty(e,t,tb(i,u,c,s));return!p.ok&&p.code&&"busy"!==p.code&&el({code:p.code,error:p.error||"Couldn't regenerate this image."}),{ok:p.ok}}
1,[th,tb,ty,tp]),tC=(0,l.useCallback)((e,t,r)=>{var l,n,a,o;let i=ec.current.find(t=>t.laneId===e),u=null==i||null==(l=i.builds)?void 0:l[t],c=null!=(a=null==u?void 0:u.versions)?a:[],s=c[r];if(!s)return;let d=c.filter((e,t)=>t!==r),f=Array.isArray(null==u?void 0:u.assets)&&(null!=(o=null==u||null==(n=u.assets)?void 0:n.length)?o:0)>0?[{assets:u.assets,summary:"Restored an earlier version",at:Date.now()},...d].slice(0,8):d;tp(e,t,{assets:s.assets,status:"done",generating:!1,build:{status:"completed",reason:null},versions:f})},[tp]),tA=(0,l.useCallback)(async()=>{if(el(null),!ea.current)return void el({code:"upgrade",error:"Generating the images unlocks on a paid plan."});let e=ec.current.filter(H);for(let t of e){let r=await tk(t.laneId);if(!r.ok&&("quota_images"===r.code||"quota_briefs"===r.code||"budget"===r.code||"trial"===r.code||"upgrade"===r.code||"insufficient_credits"===r.code)){el({code:r.code,error:r.error||"Out of credits â top up to keep creating."});let l=new Set(e.slice(e.indexOf(t)+1).map(e=>e.laneId));Z(e=>e.map(e=>l.has(e.laneId)&&("idle"===e.status||"failed"===e.status)?{...e,status:"skipped"}:e));break}}},[tk]),tP=(0,l.useCallback)(async e=>{let t=await tk(e);!t.ok&&t.code&&"no_angle"!==t.code&&"no_product"!==t.code&&"network"!==t.code?el({code:t.code,error:t.error||"Couldn't generate."}):!t.ok&&t.error&&el({code:t.code||"error",error:t.error})},[tk]),tM=(0,l.useCallback)(async e=>{let t=await tk(e,!0);!t.ok&&t.code&&"busy"!==t.code&&el({code:t.code,error:t.error||"Couldn't regenerate."})},[tk]),tS=(0,l.useCallback)(async e=>{let t=ec.current.find(t=>t.laneId===e);if(!t||ee)return;let r=t.builds?Array.from(new Set(Object.values(t.builds).map(e=>e.briefId).filter(e=>!!e))):t.briefId?[t.briefId]:[];if(0!==r.length){et(e);try{let t=(await Promise.all(r.map(async e=>{let t=await fetch("/api/app2/create",{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({briefId:e})}),r=await t.json().catch(()=>({}));return t.ok&&!1!==r.ok?{ok:!0}:{ok:!1,error:r.error}}))).find(e=>!e.ok);t?el({code:"approve",error:t.error||"Couldn't save â try again."}):eg(e,{approved:!0})}catch(e){el({code:"approve",error:"Network error â couldn't save."})}finally{et(null)}}},[ee,eg]);return{lanes:Y,analyzing:V,laneAnalyzing:Q,assignProduct:eO,toggleProduct:eT,setProductLayout:e_,setProductModelMode:ex,setModelCast:eD,setOutputMode:eB,setOutputKind:eH,setAspectRatio:eK,setOutputCount:eE,overrideBrief:tl,editBlock:tn,toggleBlock:ta,setCustomAngle:tu,addCustomLane:tc,removeLane:tf,addReferenceLane:ts,startProductLane:td,laneCount:Y.length,maxLanes:12,reanalyzeLane:eh,generateLane:tP,generateSize:tI,regenerateLane:tM,generateAll:tA,approve:tS,approvingId:ee,blocked:er,clearBlocked:()=>el(null),toggleSize:eq,removeSize:eG,fillSlot:eJ,addSlot:eF,removeSlot:e$,clearSlot:tr,setHero:eY,setIncludeLogo:eZ,setIncludePrice:eV,setDirection:eW,setCdAnswer:eQ,addAnnotation:eX,removeAnnotation:e0,setAnnotateTarget:e1,markBucketAnnotationsApplied:e2,setReviewText:e4,setStarCount:e5,setReviewerName:e3,setCutout:e6,setTypographyAsset:e9,setBackgroundAsset:e8,setModelAsset:e7,toggleSkipSlot:te,claimSlot:tt,generateBriefPreview:to,toggleOutputKind:ti,regenerateBucket:tw,restoreBucketVersion:tC,saveStatus:eb,otherTabEditing:ek}}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.