{"version":3,"file":"SliceSimulator.cjs","names":["SimulatorManager","useState","getDefaultMessage","useRouter","getSlices","StateEventType","lzString","SliceSimulatorWrapper"],"sources":["../src/SliceSimulator.tsx"],"sourcesContent":["\"use client\"\n\nimport { SimulatorManager, StateEventType, getDefaultMessage } from \"@prismicio/simulator/kit\"\nimport type { SliceSimulatorProps as BaseSliceSimulatorProps } from \"@prismicio/simulator/kit\"\nimport lzString from \"lz-string\"\nimport { useRouter } from \"next/navigation\"\nimport { useEffect, useState } from \"react\"\nimport type { FC, ReactNode } from \"react\"\n\nimport { getSlices } from \"./getSlices\"\nimport { SliceSimulatorWrapper } from \"./SliceSimulatorWrapper\"\n\nconst STATE_PARAMS_KEY = \"state\"\n\nconst simulatorManager = new SimulatorManager()\n\n/** Parameters provided to the Slice Simulator page. */\nexport type SliceSimulatorParams = {\n\tsearchParams: Promise<{\n\t\tstate?: string\n\t}>\n}\n\nexport type SliceSimulatorProps = BaseSliceSimulatorProps & {\n\tchildren: ReactNode\n\tclassName?: string\n}\n\n/**\n * Simulate slices in isolation. The slice simulator enables live slice development in Slice Machine\n * and live previews in the Page Builder.\n */\nexport const SliceSimulator: FC<SliceSimulatorProps> = ({\n\tchildren,\n\tbackground,\n\tzIndex,\n\tclassName,\n}) => {\n\tconst [message, setMessage] = useState(() => getDefaultMessage())\n\tconst [trustedState, setTrustedState] = useState<string | null>(null)\n\tconst router = useRouter()\n\n\tconst state =\n\t\ttypeof window !== \"undefined\"\n\t\t\t? new URL(window.location.href).searchParams.get(STATE_PARAMS_KEY)\n\t\t\t: undefined\n\t// Only render state this page received from Slice Machine or the Page\n\t// Builder. The `state` URL parameter on its own can be set by anyone.\n\tconst hasSlices = state != null && state === trustedState && getSlices(state).length > 0\n\n\tuseEffect(() => {\n\t\t// `@prismicio/simulator` accepts messages from any origin in development,\n\t\t// so the URL is as trustworthy as a message there. Trusting it keeps\n\t\t// slices visible when the dev server fully reloads the page. It is set\n\t\t// after hydration so the first render matches the server.\n\t\tif (process.env.NODE_ENV === \"development\") {\n\t\t\t// oxlint-disable-next-line react/set-state-in-effect\n\t\t\tsetTrustedState(new URL(window.location.href).searchParams.get(STATE_PARAMS_KEY))\n\t\t}\n\t}, [])\n\n\tuseEffect(() => {\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Slices,\n\t\t\t(newSlices) => {\n\t\t\t\tconst nextState = lzString.compressToEncodedURIComponent(JSON.stringify(newSlices))\n\t\t\t\tconst url = new URL(window.location.href)\n\t\t\t\turl.searchParams.set(STATE_PARAMS_KEY, nextState)\n\n\t\t\t\twindow.history.replaceState(null, \"\", url)\n\t\t\t\tsetTrustedState(nextState)\n\t\t\t\t// Wait until the next tick to prevent URL state race conditions.\n\t\t\t\tsetTimeout(() => router.refresh(), 0)\n\t\t\t},\n\t\t\t\"simulator-slices\",\n\t\t)\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Message,\n\t\t\t(newMessage) => setMessage(newMessage),\n\t\t\t\"simulator-message\",\n\t\t)\n\n\t\tsimulatorManager.init()\n\n\t\treturn () => {\n\t\t\tsimulatorManager.state.off(StateEventType.Slices, \"simulator-slices\")\n\n\t\t\tsimulatorManager.state.off(StateEventType.Message, \"simulator-message\")\n\t\t}\n\t}, [router])\n\n\treturn (\n\t\t<SliceSimulatorWrapper\n\t\t\tmessage={message}\n\t\t\thasSlices={hasSlices}\n\t\t\tbackground={background}\n\t\t\tzIndex={zIndex}\n\t\t\tclassName={className}\n\t\t>\n\t\t\t{children}\n\t\t</SliceSimulatorWrapper>\n\t)\n}\n"],"mappings":";;;;;;;;;;;AAYA,MAAM,mBAAmB;AAEzB,MAAM,mBAAmB,IAAIA,yBAAAA,iBAAiB;;;;;AAkB9C,MAAa,kBAA2C,EACvD,UACA,YACA,QACA,gBACK;CACL,MAAM,CAAC,SAAS,eAAA,GAAcC,MAAAA,SAAAA,QAAAA,GAAeC,yBAAAA,kBAAAA,CAAkB,CAAC;CAChE,MAAM,CAAC,cAAc,oBAAA,GAAmBD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