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