{
  "version": 3,
  "sources": ["../../../src/components/editor/use-revisions-url-sync.js"],
  "sourcesContent": ["import { useSelect, useDispatch } from '@wordpress/data';\nimport { useEffect, useRef } from '@wordpress/element';\nimport { store as editorStore } from '@wordpress/editor';\nimport { privateApis as routerPrivateApis } from '@wordpress/router';\nimport { addQueryArgs } from '@wordpress/url';\nimport { unlock } from '../../lock-unlock';\n\nconst { useHistory, useLocation } = unlock( routerPrivateApis );\n\n/**\n * Safari throws after more than 100 History API calls in 30 seconds.\n */\nconst URL_WRITE_DEBOUNCE_MS = 300;\n\nexport default function useRevisionsURLSync( enabled, postType, postId ) {\n\tconst location = useLocation();\n\tconst history = useHistory();\n\tconst { currentPostType, currentPostId, currentRevisionId } = useSelect(\n\t\t( select ) => {\n\t\t\tconst editor = select( editorStore );\n\t\t\treturn {\n\t\t\t\tcurrentPostType: editor.getCurrentPostType(),\n\t\t\t\tcurrentPostId: editor.getCurrentPostId(),\n\t\t\t\tcurrentRevisionId: unlock( editor ).getCurrentRevisionId(),\n\t\t\t};\n\t\t},\n\t\t[]\n\t);\n\tconst { openRevision, setCurrentRevisionId } = unlock(\n\t\tuseDispatch( editorStore )\n\t);\n\tconst entityKey = postType && postId ? `${ postType }:${ postId }` : null;\n\tconst isCurrentEntity =\n\t\t!! entityKey &&\n\t\tcurrentPostType === postType &&\n\t\tString( currentPostId ) === String( postId );\n\n\t// The route can change before the editor store. Wait until both point to the\n\t// same entity so a revision from the previous entity is not copied into the\n\t// new URL.\n\tconst handledEntityKeyRef = useRef( null );\n\tconst lastURLWriteTimeRef = useRef( null );\n\tuseEffect( () => {\n\t\tif ( ! enabled ) {\n\t\t\thandledEntityKeyRef.current = null;\n\t\t\treturn;\n\t\t}\n\t\tif (\n\t\t\t! isCurrentEntity ||\n\t\t\t! entityKey ||\n\t\t\thandledEntityKeyRef.current === entityKey\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tlastURLWriteTimeRef.current = null;\n\t\thandledEntityKeyRef.current = entityKey;\n\t\tconst revision = Number( location.query.revision );\n\t\tif ( Number.isInteger( revision ) && revision > 0 ) {\n\t\t\topenRevision( revision );\n\t\t} else if ( currentRevisionId ) {\n\t\t\tsetCurrentRevisionId( null );\n\t\t}\n\t}, [\n\t\tenabled,\n\t\tisCurrentEntity,\n\t\tentityKey,\n\t\tlocation.query.revision,\n\t\tcurrentRevisionId,\n\t\topenRevision,\n\t\tsetCurrentRevisionId,\n\t] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\t! enabled ||\n\t\t\t! isCurrentEntity ||\n\t\t\t! entityKey ||\n\t\t\thandledEntityKeyRef.current !== entityKey\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tif ( lastURLWriteTimeRef.current === null ) {\n\t\t\tlastURLWriteTimeRef.current = Date.now();\n\t\t}\n\t\tconst revisionArg = currentRevisionId\n\t\t\t? String( currentRevisionId )\n\t\t\t: undefined;\n\t\tif ( location.query.revision === revisionArg ) {\n\t\t\treturn;\n\t\t}\n\t\tconst write = async () => {\n\t\t\t// Route matching can lag behind the address bar. Writing the stale\n\t\t\t// match back would undo the navigation.\n\t\t\tconst addressBarArg =\n\t\t\t\tnew URLSearchParams( window.location.search ).get(\n\t\t\t\t\t'revision'\n\t\t\t\t) ?? undefined;\n\t\t\tif ( addressBarArg !== location.query.revision ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tlastURLWriteTimeRef.current = Date.now();\n\t\t\t// Passing `undefined` removes `revision` without dropping the other\n\t\t\t// query args.\n\t\t\ttry {\n\t\t\t\tawait history.navigate(\n\t\t\t\t\taddQueryArgs( location.path, { revision: revisionArg } ),\n\t\t\t\t\t{ replace: true }\n\t\t\t\t);\n\t\t\t} catch {\n\t\t\t\t// Leave the URL unchanged. The effect can try again on the next\n\t\t\t\t// state change.\n\t\t\t}\n\t\t};\n\t\tif (\n\t\t\tDate.now() - lastURLWriteTimeRef.current >=\n\t\t\tURL_WRITE_DEBOUNCE_MS\n\t\t) {\n\t\t\twrite();\n\t\t\treturn;\n\t\t}\n\t\tconst timeoutId = setTimeout( write, URL_WRITE_DEBOUNCE_MS );\n\t\treturn () => clearTimeout( timeoutId );\n\t}, [\n\t\tenabled,\n\t\tisCurrentEntity,\n\t\tentityKey,\n\t\tcurrentRevisionId,\n\t\tlocation,\n\t\thistory,\n\t] );\n}\n"],
  "mappings": ";AAAA,SAAS,WAAW,mBAAmB;AACvC,SAAS,WAAW,cAAc;AAClC,SAAS,SAAS,mBAAmB;AACrC,SAAS,eAAe,yBAAyB;AACjD,SAAS,oBAAoB;AAC7B,SAAS,cAAc;AAEvB,IAAM,EAAE,YAAY,YAAY,IAAI,OAAQ,iBAAkB;AAK9D,IAAM,wBAAwB;AAEf,SAAR,oBAAsC,SAAS,UAAU,QAAS;AACxE,QAAM,WAAW,YAAY;AAC7B,QAAM,UAAU,WAAW;AAC3B,QAAM,EAAE,iBAAiB,eAAe,kBAAkB,IAAI;AAAA,IAC7D,CAAE,WAAY;AACb,YAAM,SAAS,OAAQ,WAAY;AACnC,aAAO;AAAA,QACN,iBAAiB,OAAO,mBAAmB;AAAA,QAC3C,eAAe,OAAO,iBAAiB;AAAA,QACvC,mBAAmB,OAAQ,MAAO,EAAE,qBAAqB;AAAA,MAC1D;AAAA,IACD;AAAA,IACA,CAAC;AAAA,EACF;AACA,QAAM,EAAE,cAAc,qBAAqB,IAAI;AAAA,IAC9C,YAAa,WAAY;AAAA,EAC1B;AACA,QAAM,YAAY,YAAY,SAAS,GAAI,QAAS,IAAK,MAAO,KAAK;AACrE,QAAM,kBACL,CAAC,CAAE,aACH,oBAAoB,YACpB,OAAQ,aAAc,MAAM,OAAQ,MAAO;AAK5C,QAAM,sBAAsB,OAAQ,IAAK;AACzC,QAAM,sBAAsB,OAAQ,IAAK;AACzC,YAAW,MAAM;AAChB,QAAK,CAAE,SAAU;AAChB,0BAAoB,UAAU;AAC9B;AAAA,IACD;AACA,QACC,CAAE,mBACF,CAAE,aACF,oBAAoB,YAAY,WAC/B;AACD;AA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