((t,e)=>{let l=[];const a=[],r=[],i=[];let n,s,c;const g=()=>{l=e(".rl-gallery-widget"),l.length>0&&e.each(l,(t,l)=>{const n=e(l).attr("id").match(/\-\d*\-/);if(null!=n){const t=n.shift().replace(/-/g,"");a[t]=[],r[t]=e(l).find(".rl-gallery-ids"),i[t]=e(l).find(".rl-gallery-images"),i[t].sortable({items:"li.rl-gallery-image",cursor:"move",scrollSensitivity:40,forcePlaceholderSize:!0,forceHelperSize:!1,helper:"clone",opacity:.65,placeholder:"rl-gallery-sortable-placeholder",start:(t,e)=>{e.item.css("border-color","#f6f6f6")},stop:(t,e)=>{e.item.removeAttr("style")},update:(l,a)=>{const n=[];e(i[t]).find("li.rl-gallery-image").each((t,l)=>{const a=e(l).attr("data-attachment_id");n.push(a)}),r[t].val(n.join(",")),r[t].trigger("change")}}),e(i[t]).on("click",".rl-gallery-image-remove",l=>{const a=e(l.currentTarget).closest("li.rl-gallery-image");let i=r[t].val().split(",").map(t=>parseInt(t));return i=_.without(i,parseInt(a.data("attachment_id"))),a.remove(),r[t].val(i.join(",")),r[t].trigger("change"),!1})}}),e(".rl-gallery-widget-select").on("click",t=>{t.preventDefault();const l=e(t.currentTarget).closest(".rl-gallery-widget");let n=0;const s=e(l).attr("id").match(/\-\d*\-/);if(null!=s){if(n=s.shift().replace(/-/g,""),r[n].val(),0!=a[n].length)return void a[n].open();a[n]=wp.media({title:rlArgsWidgets.textSelectImages,multiple:!0,button:{text:rlArgsWidgets.textUseImages},library:{type:"image"}}),a[n].on("open",()=>{const t=a[n].state().get("selection"),l=r[n].val().split(",");e.each(l,(e,l)=>{if(!isNaN(parseFloat(l))&&isFinite(l)){const e=wp.media.attachment(l);e.fetch(),t.add(e?[e]:[])}})}),a[n].on("select",()=>{const t=a[n].state().get("selection"),l=[],s=r[n].val().split(",").map(t=>parseInt(t));t&&t.map(t=>{if(t.id){if(l.push(t.id),-1!==e.inArray(t.id,s))return;s.push(t.id),(t=t.toJSON()).sizes&&t.sizes.thumbnail&&(t.url=t.sizes.thumbnail.url),i[n].append(`\n\t\t\t\t\t\t\t\t\t<li class="rl-gallery-image" data-attachment_id="${t.id}">\n\t\t\t\t\t\t\t\t\t\t<div class="rl-gallery-inner"><div class="centered"><img src="${t.url}" /></div></div>\n\t\t\t\t\t\t\t\t\t\t<div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-remove dashicons-before dashicons-no" title="${rlArgsWidgets.textRemoveImage}"></a></div>\n\t\t\t\t\t\t\t\t\t</li>`)}});let c=s;for(let a=0;a<s.length;a++)-1===e.inArray(s[a],l)&&(e(i[n]).find(`li.rl-gallery-image[data-attachment_id="${s[a]}"]`).remove(),c=_.without(c,s[a]));r[n].val(_.uniq(c).join(",")),r[n].trigger("change")}),a[n].open()}})},o=()=>{e(".rl-image-widget-select").on("click",t=>{const l=e(t.currentTarget);s=l.parent().find(".rl-image-widget-content"),c=l.parent().find(".rl-image-widget-image-id"),t.preventDefault(),n=wp.media({title:rlArgsWidgets.textSelectImage,button:{text:rlArgsWidgets.textUseImage},library:{type:"image"},multiple:!1}),n.on("select",()=>{const t=n.state().get("selection").first().toJSON();s.html(`<img src="${t.url}" alt="" />`),c.val(t.id),c.trigger("change")}),n.on("open",()=>{const t=n.state().get("selection"),e=wp.media.attachment(c.val());e.fetch(),t.add(e?[e]:[])}),n.open()}),e(".rl-image-link-to").on("change",t=>{"custom"===e(t.currentTarget).val()?e(t.currentTarget).parent().next().slideDown("fast"):e(t.currentTarget).parent().next().slideUp("fast")})};e(()=>{g(),o()}),e(document).on("widget-updated widget-added",()=>{g(),o()})})(window,jQuery);
