{
  "version": 3,
  "sources": ["../../src/utils/waveform-player.js"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\nimport { useEvent, useRefEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { initWaveformPlayer } from './waveform-utils';\n\nconst EMPTY_ARTIST_PLACEHOLDER = '\\u00a0';\n\n/**\n * Update a live waveform player's metadata elements in place.\n *\n * The title element always exists, so the title is updated in place. The\n * subtitle element is seeded during editor player creation, so it can be\n * updated in place and hidden when the track has no artist. The artwork\n * element only exists when the track had an image when the player was created,\n * so its value is updated in place here; adding or removing an image (which\n * creates or tears down that element) is instead handled by recreating the\n * player, keyed on the `hasImage` dependency.\n *\n * The library's only metadata API is `loadTrack()`, which re-fetches and\n * re-decodes the audio and regenerates the waveform (resetting playback), so\n * it's unsuitable for live metadata edits. We instead write to the title,\n * subtitle, and artwork elements directly, which is what `loadTrack()` itself\n * does internally for these fields.\n *\n * @param {Object} instance        - The waveform player instance.\n * @param {Object} metadata        - The track metadata.\n * @param {string} metadata.title  - The track title.\n * @param {string} metadata.artist - The artist name.\n * @param {string} metadata.image  - The artwork image URL.\n */\nfunction updatePlayerMetadata( instance, { title, artist, image } ) {\n\tif ( instance.titleEl ) {\n\t\tinstance.titleEl.textContent = title ?? '';\n\t}\n\tif ( instance.subtitleEl ) {\n\t\tinstance.subtitleEl.textContent = artist ?? '';\n\t\tinstance.subtitleEl.style.display = artist ? '' : 'none';\n\t}\n\tif ( instance.artworkEl && image ) {\n\t\tinstance.artworkEl.src = image;\n\t}\n}\n\n/**\n * A reusable WaveformPlayer component for the block editor.\n *\n * Renders an audio waveform visualization with play/pause controls.\n * Automatically inherits colors from the parent block's text color.\n *\n * @param {Object}   props               - Component props.\n * @param {string}   props.src           - The audio file URL.\n * @param {string}   props.title         - The track title.\n * @param {string}   props.artist        - The artist name.\n * @param {string}   props.image         - The artwork image URL.\n * @param {string}   props.waveformStyle - Waveform style (bars, mirror, line, blocks, dots, seekbar).\n * @param {Function} props.onEnded       - Callback when the track finishes playing.\n * @return {Element} The WaveformPlayer element.\n */\nexport function WaveformPlayer( {\n\tsrc,\n\ttitle,\n\tartist,\n\timage,\n\twaveformStyle,\n\tonEnded,\n} ) {\n\t// Store onEnded in a stable callback so it doesn't need to be a useRefEffect dependency.\n\t// The callback changes reference on every render (its dependency chain\n\t// includes an unstable array), which would cause useRefEffect to destroy\n\t// and recreate the entire player on every re-render, making it disappear\n\t// during editor resizes.\n\tconst onEndedEvent = useEvent( onEnded );\n\tconst metadataRef = useRef( { title, artist, image } );\n\tconst playerRef = useRef();\n\n\t// The artwork element only exists when an image was present when the\n\t// player was created. Recreate the player when one is added or removed so\n\t// that element is created or torn down; value changes to an existing\n\t// element are applied in place below.\n\tconst hasImage = !! image;\n\n\t// Keep the freshest metadata available to init() (which runs on a\n\t// deferred timeout) and update the live player in place when metadata\n\t// changes. Updating in place avoids destroying and recreating the\n\t// player, which would flash it on every keystroke while editing a\n\t// track's title or artist.\n\tuseEffect( () => {\n\t\tmetadataRef.current = { title, artist, image };\n\n\t\tconst instance = playerRef.current?.instance;\n\t\tif ( instance ) {\n\t\t\tupdatePlayerMetadata( instance, { title, artist, image } );\n\t\t}\n\t}, [ title, artist, image ] );\n\n\tconst ref = useRefEffect(\n\t\t( element ) => {\n\t\t\tif ( ! src ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet cancelled = false;\n\t\t\tlet playerDestroy;\n\n\t\t\tfunction init() {\n\t\t\t\tif ( cancelled ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst player = initWaveformPlayer( element, {\n\t\t\t\t\tsrc,\n\t\t\t\t\t...metadataRef.current,\n\t\t\t\t\twaveformStyle,\n\t\t\t\t\tartist:\n\t\t\t\t\t\tmetadataRef.current.artist || EMPTY_ARTIST_PLACEHOLDER,\n\t\t\t\t\tonEnded: () => onEndedEvent?.(),\n\t\t\t\t} );\n\t\t\t\tplayerRef.current = player;\n\t\t\t\tupdatePlayerMetadata( player.instance, metadataRef.current );\n\t\t\t\tconst { destroy } = player;\n\t\t\t\tplayerDestroy = destroy;\n\t\t\t}\n\n\t\t\t// Defer initialization so the element inherits the correct\n\t\t\t// text color, which is used to derive waveform colors. In the\n\t\t\t// editor iframe, theme styles (CSS custom properties) are\n\t\t\t// injected dynamically, so getComputedStyle may return the\n\t\t\t// default black on first render.\n\t\t\t// Using a requestAnimationFrame loop isn't sufficient to solve the issue.\n\t\t\t// TODO - find a better option than a setTimeout, so we're not relying on an arbitrary number.\n\t\t\tconst timeoutId = setTimeout( init, 100 );\n\n\t\t\treturn () => {\n\t\t\t\tcancelled = true;\n\t\t\t\tclearTimeout( timeoutId );\n\t\t\t\tplayerRef.current = undefined;\n\t\t\t\tplayerDestroy?.();\n\t\t\t};\n\t\t},\n\t\t[ onEndedEvent, src, waveformStyle, hasImage ]\n\t);\n\n\treturn <div ref={ ref } className=\"wp-block-playlist__waveform-player\" />;\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAkC;AAClC,qBAAuC;AAKvC,4BAAmC;AA0I3B;AAxIR,IAAM,2BAA2B;AAyBjC,SAAS,qBAAsB,UAAU,EAAE,OAAO,QAAQ,MAAM,GAAI;AACnE,MAAK,SAAS,SAAU;AACvB,aAAS,QAAQ,cAAc,SAAS;AAAA,EACzC;AACA,MAAK,SAAS,YAAa;AAC1B,aAAS,WAAW,cAAc,UAAU;AAC5C,aAAS,WAAW,MAAM,UAAU,SAAS,KAAK;AAAA,EACnD;AACA,MAAK,SAAS,aAAa,OAAQ;AAClC,aAAS,UAAU,MAAM;AAAA,EAC1B;AACD;AAiBO,SAAS,eAAgB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAI;AAMH,QAAM,mBAAe,yBAAU,OAAQ;AACvC,QAAM,kBAAc,uBAAQ,EAAE,OAA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