(()=>{"use strict";function e(e){return btoa(encodeURIComponent(e).replace(/%([0-9A-F]{2})/g,function(e,n){return String.fromCharCode("0x"+n)}))}var n,i,o;n=jQuery,i=window.ModuleBuilderModal,o={init:function(){n(".insert-file-row").after('<div class="igd-dokan"><button type="button" class="dokan-btn dokan-btn-default igd-wc-button" title="Add Files from Google Drive"><img src="'.concat(igd.pluginUrl,'/assets/images/drive.png" width="20"/><span>').concat(wp.i18n.__("Add File","integrate-google-drive"),"</span></button></div>")),n(document).on("click",".dokan-btn.igd-wc-button",o.handleDokanFileSelector),n(document).on("click",".save-upload-settings",o.saveDokanUploadSettings),n(document).on("click",".dokan-igd-settings .remove-account",o.handleDokanAccountRemove),n(document).on("click","#igd-dokan-select-media-folders",o.selectMediaLibraryFolders),n(document).on("click",".save-media-library-settings",o.saveMediaLibrarySettings)},saveMediaLibrarySettings:function(){var e=this;wp.ajax.send("igd_save_dokan_media_library_settings",{data:{data:n("#igd-dokan-media-library-settings").serialize(),nonce:igd.nonce},beforeSend:function(){n(e).find("i").removeClass("fa-save").addClass("fa-spinner fa-spin")},success:function(e){n(".dokan-igd-media-library-ajax-response").show()},error:function(e){console.log(e)},complete:function(){n(e).find("i").removeClass("fa-spin fa-spinner").addClass("fa-save")}})},selectMediaLibraryFolders:function(){Swal.fire({html:'<div id="igd-select-files" class="igd-module-builder-modal-wrap"></div>',showConfirmButton:!1,customClass:{container:"igd-swal igd-module-builder-modal-container"},didOpen:function(o){var t,a=document.getElementById("igd-select-files"),d=null===(t=n("#igd_dokan_media_folders").val())||void 0===t?void 0:t.trim(),s=d&&""!==d&&'""'!==d?JSON.parse(d):[];ReactDOM.render(React.createElement(i,{initData:{folders:s},onUpdate:function(i){var o=i.folders,t=(void 0===o?[]:o).map(function(e){return{id:e.id,accountId:e.accountId,name:e.name,iconLink:e.iconLink}});n("#igd_dokan_media_folders").val(JSON.stringify(t)),e(t),Swal.close()},onClose:function(){return Swal.close()},isSelectFiles:!0,selectionType:"folders"}),a)},willClose:function(e){var n=document.getElementById("igd-select-files");ReactDOM.unmountComponentAtNode(n)}});var e=function(e){var i=n(".media-folders");i.empty(),e.map(function(e){var n=e.id,o=e.name;i.append('\n                    <div class="folder-item" data-id="'.concat(n,'">\n                        <img src="').concat(igd.pluginUrl,'/assets/images/icons/folder.png}" />\n                        ').concat(o,"\n                    </div>\n                    "))})}},handleDokanAccountRemove:function(){var e=this;Swal.fire({title:wp.i18n.__("Are you sure?","integrate-google-drive"),text:wp.i18n.__("You won't be able to revert this!","integrate-google-drive"),icon:"warning",showCancelButton:!0,confirmButtonText:wp.i18n.__("Yes, remove it!","integrate-google-drive"),reverseButtons:!0,showLoaderOnConfirm:!0,customClass:{container:"igd-swal igd-swal-reverse"},preConfirm:function(){var i=n(e).closest(".igd-account-item").data("id");return wp.ajax.post("igd_delete_account",{id:i,nonce:igd.nonce})}}).then(function(i){i.value&&(n(e).closest(".igd-account-item").remove(),Swal.fire({title:wp.i18n.__("Removed!","integrate-google-drive"),text:wp.i18n.__("Account has been removed.","integrate-google-drive"),icon:"success",showConfirmButton:!1,timer:1500,toast:!0}))})},handleDokanFileSelector:function(o){var t=n(this).parents(".dokan-download-wrapper");Swal.fire({html:'<div id="igd-select-files" class="igd-module-builder-modal-wrap"></div>',showConfirmButton:!1,customClass:{container:"igd-swal igd-module-builder-modal-container"},didOpen:function(o){ReactDOM.render(React.createElement(i,{initData:{},onUpdate:function(i){var o=i.folders,a=void 0===o?[]:o,d=i.woocommerceRedirect,s=i.woocommerceAddPermission,r=t.find("tbody"),c=t.find(".insert-file-row ").data("row");a.map(function(i){var o=i.id,t=i.name,a=i.type,l=i.accountId,u=e(JSON.stringify({id:o,type:a,account_id:l,redirect:d,create_permission:s})),m="https://drive.google.com/igd-wc-download/".concat(u),f=n(c);f.find('[name="_wc_file_names[]"]').val(t),f.find('[name="_wc_file_urls[]"]').val(m),r.append(f)}),Swal.close()},onClose:function(){return Swal.close()},isWooCommerce:"download"}),document.getElementById("igd-select-files"))},willClose:function(e){ReactDOM.unmountComponentAtNode(document.getElementById("igd-select-files"))}})},saveDokanUploadSettings:function(){var e=this;wp.ajax.send("igd_save_dokan_upload_settings",{data:{data:n("#igd-dokan-upload-settings").serialize(),nonce:igd.nonce},beforeSend:function(){n(e).find("i").removeClass("fa-save").addClass("fa-spinner fa-spin")},success:function(e){n(".dokan-igd-upload-ajax-response").show()},error:function(e){console.log(e)},complete:function(){n(e).find("i").removeClass("fa-spin fa-spinner").addClass("fa-save")}})}},n(document).ready(o.init)})();