import{a as e,b as a,m as n,Q as l,A as r,S as t,K as o,L as i}from"../../vendor.js";import{useReducer as s,useEffect as c}from"react";import{a as u}from"../../hooks.js";var d=function(d){var v=d.className,g=d.value,m=d.onChange,f=d.accept,p=void 0===f?["image/jpeg","image/jpg","image/png","image/svg+xml"]:f,k=u().events,x=s(e,{value:g||"",loading:!1}),h=a(x,2),j=h[0],b=h[1];c((function(){g&&b({value:g})}),[g]);var N=function(){var e=o(i.mark((function e(a){var n,l,r;return i.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!j.loading){e.next=2;break}return e.abrupt("return");case 2:if(b({loading:!0}),null==k||!k.onImageUpload){e.next=10;break}return e.next=6,k.onImageUpload(event);case 6:n=e.sent,w(n),e.next=12;break;case 10:(l=a.target.files[0])?((r=new FileReader).readAsDataURL(l),r.onload=function(){w({url:r.result})}):b({loading:!1});case 12:case"end":return e.stop()}}),e)})));return function(a){return e.apply(this,arguments)}}(),w=function(){var e=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{}).url;b({value:e}),null==m||m({value:e}),b({loading:!1})};return n.jsx(l,{className:r("oak-core-image-field",v),disabled:j.loading,onClick:j.value?function(e){e.preventDefault(),b({value:null}),null==m||m({value:null})}:function(e){if(e.preventDefault(),!j.loading){var a=document.createElement("input");a.type="file",a.accept=p.join(","),a.addEventListener("change",N,!1),a.click()}},style:{backgroundImage:j.value&&"url(".concat(j.value,")")},children:j.loading?n.jsx(t,{}):j.value?n.jsx("div",{className:"oak-remove",children:n.jsx("i",{className:"oak-icons",children:"close"})}):n.jsx("i",{className:"oak-icons",children:"add"})})};export{d as C};
//# sourceMappingURL=index.js.map
