"use strict";var e=require("tslib"),r=require("uuid"),t=require("react"),n=require("@frameright/image-display-control-metadata-parser");function i(e){var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var a=i(t);const o="undefined"==typeof window,s=!o;let c=!1;try{c=void 0!==process.env.JEST_WORKER_ID}catch(e){}s&&!c&&import("@frameright/image-display-control-web-component/dist/src/image-display-control.js");let l=null;function d(e){const r=new n.Parser(e).getIdcMetadata("rectangle","crop");return JSON.stringify(r)}function u(e){var r,t;if(!function(e){var r;return"props"in e?!!e.props.src:"attributes"in e&&!!(null===(r=e.attributes.getNamedItem("src"))||void 0===r?void 0:r.value)}(e))return null;let n=null;if("props"in e){if("string"==typeof e.props.src)n={src:e.props.src};else{if(!("src"in e.props.src))return f("Unknown src= attribute format: ",e.props.src),null;n={src:e.props.src.src},"pathOnServer"in e.props.src&&(n.pathOnServer=e.props.src.pathOnServer)}e.props["data-path-on-server"]&&(n.pathOnServer=e.props["data-path-on-server"])}else n={src:(null===(r=e.attributes.getNamedItem("data-src-prop"))||void 0===r?void 0:r.value)||(null===(t=e.attributes.getNamedItem("src"))||void 0===t?void 0:t.value)||e.src};return n}function p(r){return e.__awaiter(this,void 0,void 0,(function*(){for(const e of[100,200,500,1e3,1e3]){if(l||s)return void m(r,"All imports have resolved.");m(r,"Waiting for imports to resolve..."),yield new Promise((r=>setTimeout(r,e)))}f("Some imports are still missing, image regions will not be available.")}))}s||(require?l=require("fs"):import("fs").then((e=>{l=e})));const g="[idc]";function f(...e){console.warn(g,"[warn]",...e)}function m(e,...r){e&&console.log(g,"[debug]",...r)}exports.ImageDisplayControl=function({children:t,"data-debug":n=!1}){m(n,"Rendering...");const i=t||[],[s]=a.useState((()=>r.v4())),c=a.useRef(null),[g,v]=a.useState(new Map);!function(e,r,t){m(t,"Initializing image regions map..."),a.Children.forEach(r,(r=>{m(t,"Initializing image regions map for child:",r);const n=u(r);if(n)if(e.get(n.src))m(t,"Found image regions in state for",n.src);else{let r="";o&&(n.pathOnServer?"none"!==n.pathOnServer&&(m(t,"Reading image regions on disk for",n.src),r=function(e,r){if(!l)return f("fs module not available, can't read image regions from disk."),"";let t="";try{m(r,"Reading image regions from",e),t=d(l.readFileSync(e)),m(r,"Found image regions:",t)}catch(e){f("Error while reading image regions from disk:",e)}return t}(n.pathOnServer,t)):f("Missing data-path-on-server attribute for",n.src,", can't read image regions from disk. You probably want to read https://docs.frameright.io/react/ssr")),e.set(n.src,r)}}))}(g,i,n);let h=0;const b=a.Children.map(i,(e=>{const r=u(e);if(!r)return e;++h;const t={is:"image-display-control",class:`${e.props.class||""} ${e.props.className||""}`.trim(),ref:c,"data-idc-uuid":s,suppressHydrationWarning:!0,"data-src-prop":r.src};o&&(t["data-path-on-server"]=void 0);const i=g.get(r.src);i&&(t["data-image-regions"]=i);const l=a.cloneElement(e,t);return m(n,"Rendering extended child:",l),l}));return a.useEffect((()=>{!function(r,t,n,i){e.__awaiter(this,void 0,void 0,(function*(){m(i,"Populating image regions map if needed...");const e=new Map;for(const r of function(e){const r=e.current;if(r){const e=r.dataset.idcUuid;if(e){const t=r.parentElement;if(t)return Array.from(t.querySelectorAll('[data-idc-uuid="'+e+'"]'))}}return[]}(n)){const t=u(r);if(t){m(i,"Reading image regions from DOM for",t.src,"...");const n=r.dataset.imageRegions;n&&e.set(t.src,n)}}let a=!1;for(const[t,n]of r)if(!n){const n=e.get(t);if(n)m(i,"Populating image regions from DOM for",t),r.set(t,n);else{let e;m(i,"Image regions not found in DOM for",t,", fetching...");try{const r=yield fetch(t);e=yield r.arrayBuffer()}catch(e){f("Error fetching image:",e);continue}if(r.get(t)){m(i,"Image regions for",t,"already populated meanwhile, aborting.");continue}yield p(i);const n=d(e);n&&(m(i,"Populating image regions from metadata for",t),r.set(t,n),a=!0)}}a?(m(i,"Updating image regions map in state."),t(new Map(r))):m(i,"Image regions map in state is up-to-date."),m(i,"Image regions map in state:",r)}))}(g,v,c,n),function(e){const r=e.current;if(r){const e=r.parentElement;e&&!e.dataset.idcParent&&f("Parent element of doesn't have a 'data-idc-parent' attribute. You probably want to read https://docs.frameright.io/react/attributes#parent-dom-elements-attributes")}}(c)}),[g,c,n,h]),a.createElement(a.Fragment,null,b)}; //# sourceMappingURL=react-image-display-control.min.cjs.map