/**
 * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */let formElements,activated=!1,instance;const addListeners=()=>{formElements.cropX.addEventListener("change",({currentTarget:e})=>{instance.setData({x:parseInt(e.value,10)})}),formElements.cropY.addEventListener("change",({currentTarget:e})=>{instance.setData({y:parseInt(e.value,10)})}),formElements.cropWidth.addEventListener("change",({currentTarget:e})=>{instance.setData({width:parseInt(e.value,10)})}),formElements.cropHeight.addEventListener("change",({currentTarget:e})=>{instance.setData({height:parseInt(e.value,10)})}),formElements.aspectRatio.addEventListener("change",({currentTarget:e})=>{instance.setAspectRatio(e.value)}),activated=!0},init=e=>{const t=e.naturalWidth/e.naturalHeight;!Number.isNaN(t)&&Number.isFinite(t)&&(formElements.cropAspectRatioOption.value=t),instance=new Cropper(e,{viewMode:1,responsive:!0,restore:!0,autoCrop:!0,movable:!1,zoomable:!1,rotatable:!1,autoCropArea:1,crop(a){formElements.cropX.value=Math.round(a.detail.x),formElements.cropY.value=Math.round(a.detail.y),formElements.cropWidth.value=Math.round(a.detail.width),formElements.cropHeight.value=Math.round(a.detail.height);const o=Joomla.MediaManager.Edit.original.extension==="jpg"?"jpeg":Joomla.MediaManager.Edit.original.extension,r=formElements.cropQuality.value;Joomla.MediaManager.Edit.current.contents=this.cropper.getCroppedCanvas().toDataURL(`image/${o}`,r),window.dispatchEvent(new Event("mediaManager.history.point"))}}),activated||addListeners(),instance.setAspectRatio(formElements.cropAspectRatioOption.value)};window.addEventListener("media-manager-edit-init",()=>{formElements={aspectRatio:document.getElementById("jform_aspectRatio"),cropHeight:document.getElementById("jform_crop_height"),cropWidth:document.getElementById("jform_crop_width"),cropY:document.getElementById("jform_crop_y"),cropX:document.getElementById("jform_crop_x"),cropQuality:document.getElementById("jform_crop_quality"),cropAspectRatioOption:document.querySelector(".crop-aspect-ratio-option")},Joomla.MediaManager.Edit.plugins.crop={Activate(e){return new Promise(t=>{init(e),t()})},Deactivate(e){return new Promise(t=>{e.cropper&&(e.cropper.destroy(),instance=null),t()})}}},{once:!0});
