{"version":3,"file":"internal-8328aafb.cjs","sources":["../../src/internal.ts"],"sourcesContent":["import type { InlineOptions } from '@twind/core'\nimport { tw as tw$, identity, consume, stringify, noop } from '@twind/core'\n\nimport diff from 'fast-diff'\n\nexport function createState(options: InlineOptions['tw'] | InlineOptions = {}) {\n  const { tw = tw$, minify = identity } =\n    typeof options == 'function' ? ({ tw: options } as InlineOptions) : options\n\n  let buffer = ''\n  let lastStyle: string | null = null\n  let restoreCurrentState = noop\n\n  return {\n    push: (chunk: string): boolean => {\n      buffer += chunk\n      return lastStyle === null ? isShellReady(buffer) : isSuspenseChunkReady(buffer)\n    },\n\n    flush: (): string | undefined => {\n      if (buffer) {\n        const restore = tw.snapshot()\n\n        restoreCurrentState()\n\n        let html = consume(buffer, tw)\n        const nextStyle = minify(stringify(tw.target), html)\n\n        restoreCurrentState = tw.snapshot()\n\n        restore()\n\n        if (lastStyle === null) {\n          // inital shell is ready\n          html = html.replace('</head>', `<style data-twind>${nextStyle}</style></head>`)\n        } else {\n          // a suspense chunk\n          const styleDiff: [offset: number, text: string][] = []\n\n          let offset = 0\n          for (const [type, text] of diff(lastStyle, nextStyle)) {\n            if (type === 1) {\n              // insert\n              styleDiff.push([offset, text])\n            }\n\n            // must be equal: type === 0\n            offset += text.length\n          }\n\n          if (styleDiff.length) {\n            // add style patch script that will update the previously created style element content\n            // but only if client side twind hasn't taken over yet (data-twind=\"claimed\")\n\n            // ;(function (style, diff) {\n            //   if (style) {\n            //     style.textContent = diff.reduce(function (textContent, change) {\n            //       return textContent.slice(0, change[0]) + change[1] + textContent.slice(change[0])\n            //     }, style.textContent || '')\n            //   }\n            // })(document.querySelector('style[data-twind=\"\"]'), [])\n\n            html = `<script>!function(e,n){e&&(e.textContent=n.reduce((function(e,n){return e.slice(0,n[0])+n[1]+e.slice(n[0])}),e.textContent||''))}(document.querySelector('style[data-twind=\"\"]'),${JSON.stringify(\n              styleDiff,\n            )})</script>${html}`\n          }\n        }\n\n        buffer = ''\n        lastStyle = nextStyle\n\n        return html\n      }\n    },\n  }\n}\n\nfunction isShellReady(markup: string): boolean {\n  return markup.endsWith('</body></html>')\n}\n\nfunction isSuspenseChunkReady(markup: string): boolean {\n  return markup.endsWith('</script>')\n}\n"],"names":["options","tw","tw$","minify","identity","buffer","lastStyle","restoreCurrentState","noop","push","chunk","markup","endsWith","flush","restore","snapshot","html","consume","nextStyle","stringify","target","replace","styleDiff","offset","type","text","diff","length","JSON"],"mappings":";;;;sBAKO,SAAqBA,UAA+C,EAAE,EAAE;IAC7E,IAAM,EAAEC,IAAKC,OAAAA,CAAAA,EAAKC,QAASC,KAAAA,WAAU,GACnC,AAAkB,cAAlB,OAAOJ,UAAyB;QAAEC,IAAID;IAAQ,IAAsBA,OAAO,EAEzEK,SAAS,IACTC,YAA2B,IAAI,EAC/BC,sBAAsBC,KAAAA,IAAAA;IAE1B,OAAO;QACLC,MAAM,CAACC,QAA2B;mBAChCL,UAAUK,OACHJ,AAAc,IAAI,KAAlBA,YA8DJK,AA9DsCN,OA8D/BO,QAAQ,CAAC,oBAIhBD,AAlEqEN,OAkE9DO,QAAQ,CAAC,YAlE4D;QACjF;QAEAC,OAAO,IAA0B;YAC/B,IAAIR,QAAQ;gBACV,IAAMS,UAAUb,GAAGc,QAAQ;gBAE3BR;gBAEA,IAAIS,OAAOC,KAAAA,QAAQZ,QAAQJ,KACrBiB,YAAYf,OAAOgB,KAAUlB,SAAAA,CAAAA,GAAGmB,MAAM,GAAGJ;gBAE/CT,sBAAsBN,GAAGc,QAAQ;gBAEjCD;gBAEA,IAAIR,AAAc,IAAI,KAAlBA,WAAkB,wBAAA;gBAEpBU,OAAOA,KAAKK,OAAO,CAAC,WAAW,CAAC,kBAAkB,EAAEH,UAAU,eAAe,CAAC;qBACzE;;oBAEL,IAAMI,YAA8C,EAAE,EAElDC,SAAS;oBACb,KAAK,IAAM,CAACC,MAAMC,KAAK,IAAIC,cAAAA,OAAAA,CAAKpB,WAAWY,WAAY;wBACxC,MAATM;wBAE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