(()=>{"use strict";var i={d:(t,e)=>{for(var s in e)i.o(e,s)&&!i.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},o:(i,t)=>Object.prototype.hasOwnProperty.call(i,t),r:i=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(i,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(i,"__esModule",{value:!0})}},t={};i.r(t),i.d(t,{default:()=>e}),window.wp.url;const e=function(){return{isLoading:!1,valid:!1,links:[],maxLinks:0,rejected:"",showingSuccessMessage:!1,showUpdateNotice:!1,showInvalidSubmission:!1,isDismissingNotice:!1,async init(){const i=await AFFWP.portal.core.fetch({path:"affwp/v2/portal/integrations/direct-link-tracking/get-links",cacheResult:!1});this.links=i.links.map((i=>(i.timer=!1,i.isValidatingUrl=!1,i.isRemoving=!1,i))),this.rejected=i.rejected.join("<br>"),0===this.links.length&&this.addDomain(),this.checkValid(),this.isLoading=!1},addDomain(){this.links.length+1<=this.maxLinks&&(this.links.push({url_id:"",url:"",errors:{}}),this.valid=!1)},getLinkObject(i){return void 0!==this.links[i]&&this.links[i]},getLinkParam(i,t){const e=this.getLinkObject(i);return!1===e||void 0===e[t]?"":e[t]},async removeLink(i){const t=this.getLinkObject(i),e=t.url_id;e&&(t.isRemoving=!0,await AFFWP.portal.core.fetch({path:`affwp/v2/portal/integrations/direct-link-tracking/links/${e}`,method:"DELETE",data:{}})),this.links.splice(i,1)},async submit(){if(!this.valid)return;this.isLoading=!0;const i=await AFFWP.portal.core.fetch({path:"affwp/v2/portal/integrations/direct-link-tracking/save-links",method:"POST",data:{links:this.links}});this.showInvalidSubmission=!i.success,this.links=i.links,this.rejected=i.rejected.join("<br>"),this.showUpdateNotice=!0,this.isLoading=!1},async dismiss(i){this.isDismissingNotice||(this.isDismissingNotice=!0,this.isLoading=!0,await AFFWP.portal.core.fetch({path:"affwp/v2/portal/integrations/direct-link-tracking/dismiss-notice",method:"POST",data:{url_id:i}}),this.init())},hasError:(i,t)=>i.errors&&!0===i.errors[t],hasErrors:i=>i.errors&&Object.keys(i.errors).length>0,checkValid(){let i=!0;this.links.find((i=>i.errors&&Object.keys(i.errors).length>0))&&(i=!1),this.valid=i},validateFrontend(i){const t=this.getLinkObject(i);if(!1===t)return;const e=t.url;clearTimeout(t.timer);let s=!1;t.errors=[],""===e.trim()?(t.errors.empty=!0,s=!0):(this.links.find(((t,s)=>s!==i&&t.url===e))&&(t.errors.duplicated=!0,s=!0),function(i){return/\.\w\w.*/.test(i)}(e)||(t.errors.invalid=!0,s=!0)),s?this.checkValid():(this.valid=!1,t.timer=setTimeout(this.validateBackend.bind(this,i),500))},async validateBackend(i){const t=this.getLinkObject(i);if(!1===t)return;const e=t.url;t.isValidatingUrl=!0;const s=await AFFWP.portal.core.fetch({path:"affwp/v2/portal/integrations/direct-link-tracking/validate",method:"POST",data:{url:e}});t.isValidatingUrl=!1,e===t.url&&(s.success||(t.errors.addon=!0,t.errors.addonReason=s.error),this.checkValid())}}};((window.AFFWP=window.AFFWP||{}).portal=window.AFFWP.portal||{}).directLink=t})();