(()=>{"use strict";const e=window.wp.i18n;window.ampCustomizeControls=function(t,a){const i={data:{queryVar:"amp",panelId:"",ampUrl:"",l10n:{unavailableMessage:"",unavailableLinkText:""}},tooltipTimeout:5e3,tooltipVisible:new t.Value(!1),tooltipFocused:new t.Value(0),boot:function(e){function a(){t.panel(i.data.panelId,i.panelReady)}i.data=e,t.state?(i.addState(),t.bind("ready",a)):t.bind("ready",(function(){i.addState(),a()}))},addState:function(){t.state.add("ampEnabled",new t.Value(!1)),t.state.add("ampAvailable",new t.Value(!1))},isAmpUrl:function(e){const t=document.createElement("a"),a=new RegExp("\\/"+i.data.queryVar+"\\/?$");return t.href=e,!_.isUndefined(wp.customize.utils.parseQueryString(t.search.substr(1))[i.data.queryVar])||a.test(t.pathname)},unampifyUrl:function(e){const t=document.createElement("a"),n=new RegExp("\\/"+i.data.queryVar+"\\/?$");if(t.href=e,t.pathname=t.pathname.replace(n,""),1<t.search.length){const e=window.wp.customize.utils.parseQueryString(t.search.substr(1));delete e[i.data.queryVar],t.search=a.param(e)}return t.href},ampifyUrl:function(e){const t=document.createElement("a");return t.href=i.unampifyUrl(e),t.search.length&&(t.search+="&"),t.search+=i.data.queryVar+"=1",t.href},tryToCloseTooltip:function(){clearTimeout(i.tooltipTimeoutId),i.tooltipTimeoutId=setTimeout((function(){i.tooltipVisible.get()&&(0<i.tooltipFocused.get()?i.tryToCloseTooltip():i.tooltipVisible.set(!1))}),i.tooltipTimeout)},setCurrentAmpUrl:function(e){const a=t.state("ampEnabled").get();return!a&&i.isAmpUrl(e)?i.unampifyUrl(e):a&&!i.isAmpUrl(e)?i.ampifyUrl(e):e},updatePreviewUrl:function(){t.previewer.previewUrl.set(i.setCurrentAmpUrl(t.previewer.previewUrl.get()))},enableAndNavigateToUrl:function(e){t.state("ampEnabled").set(!0),t.previewer.previewUrl.set(e)},updatePanelNotifications:function(){const e=t.panel(i.data.panelId),a=e.sections().concat([e]);t.state("ampAvailable").get()?_.each(a,(function(e){e.notifications.remove("amp_unavailable")})):_.each(a,(function(e){e.notifications.add(new t.Notification("amp_unavailable",{message:i.data.l10n.unavailableMessage,type:"info",linkText:i.data.l10n.unavailableLinkText,url:i.data.ampUrl,templateId:"customize-amp-unavailable-notification",render(){const e=t.Notification.prototype.render.call(this);return e.find("a").on("click",(function(e){e.preventDefault(),i.enableAndNavigateToUrl(this.href)})),e}}))}))},panelReady:function(n){const o=a(wp.template("customize-amp-enabled-toggle")({message:i.data.l10n.unavailableMessage,linkText:i.data.l10n.unavailableLinkText,url:i.data.ampUrl})),l=o.find("input[type=checkbox]"),s=o.find(".tooltip"),r=s.find("a");var p;n.expanded.bind((function(e){e&&(t.state("ampAvailable").get()?t.state("ampEnabled").set(n.expanded.get()):n.notifications||setTimeout((function(){i.tooltipVisible.set(!0)}),250))})),n.notifications&&(t.state("ampAvailable").bind(i.updatePanelNotifications),i.updatePanelNotifications(),t.section.bind("add",i.updatePanelNotifications)),t.previewer.bind("amp-status",(function(e){t.state("ampAvailable").set(e.available)})),t.previewer.bind("amp-status",(function e(a){t.state("ampEnabled").set(a.enabled),t.previewer.unbind("amp-status",e)})),t.previewer.previewUrl.validate=(p=t.previewer.previewUrl.validate,function(e){let t=p.call(this,e);return t&&(t=i.setCurrentAmpUrl(t)),t}),t.state("ampEnabled").bind((function(e){if(l.prop("checked",e),i.updatePreviewUrl(),e){const e="tablet";e in t.settings.previewableDevices&&t.state("previewedDevice").set(e)}})),t.state("ampAvailable").bind((function(e){l.toggleClass("disabled",!e),t.state("ampEnabled").get()&&i.tooltipVisible.set(!e)})),a(".devices-wrapper").prepend(o);const u=a(".collapse-sidebar.button"),c=u.find("> .collapse-sidebar-label"),d=()=>{t.state("paneVisible").get()?u.prop("title",c.text()):u.prop("title",(0,e.__)("Show Controls","amp"))};d(),t.state("paneVisible").bind(d),r.on("click",(function(e){e.preventDefault(),i.enableAndNavigateToUrl(this.href)})),i.tooltipVisible.bind((function(e){s.attr("aria-hidden",e?"false":"true"),e?(a(document).on("click.amp-toggle-outside",(function(e){a.contains(o[0],e.target)||i.tooltipVisible.set(!1)})),s.fadeIn(),i.tryToCloseTooltip()):(s.fadeOut(),i.tooltipFocused.set(0),a(document).off("click.amp-toggle-outside"))})),l.on("click",(function(){this.checked=!this.checked,t.state("ampAvailable").get()?t.state("ampEnabled").set(!t.state("ampEnabled").get()):i.tooltipVisible.set(!0)})),s.on("mouseenter",(function(){t.state("ampAvailable").get()||i.tooltipVisible.set(!0),i.tooltipFocused.set(i.tooltipFocused.get()+1)})),s.on("mouseleave",(function(){i.tooltipFocused.set(i.tooltipFocused.get()-1)})),r.on("focus",(function(){t.state("ampAvailable").get()||i.tooltipVisible.set(!0),i.tooltipFocused.set(i.tooltipFocused.get()+1)})),r.on("blur",(function(){i.tooltipFocused.set(i.tooltipFocused.get()-1)}))}};return i}(wp.customize,jQuery)})();