var Teca=function(e){this.tecaEnv="https://teca-collaudo.prod.os01.ocp.cineca.it",this.env="pp",this.currentTokenUrl=e,this.tecaEventListener=null,this.afterRefreshTokenCallback=null,this.currentToken=null,this.closeAfterResourceCreation=!1,this.tecaContainer="teca-container",this.mode=null,this.libVersion="0.5.0",window.addEventListener("message",e=>{if(e?.data){var t=e?.data?.indexOf(":-:");if(this.tecaEventListener&&(!t||-1==t))this.tecaEventListener({tipoEvento:e.data,payload:null});else{var i=e.data.split(":-:"),n=i[0],r=i[1];"CREATE_RESOURCE_TASK"==n||"RESOURCE_LINKED"==n?(this.tecaEventListener&&this.tecaEventListener({tipoEvento:n,payload:{uuid:r,qualifica:i.length>=3&&"null"!=i[2]&&"undefined"!=i[2]?i[2]:null,idLogico:i.length>=4&&"null"!=i[3]&&"undefined"!=i[3]?i[3]:null,idConservatore:i.length>=5&&"null"!=i[4]&&"undefined"!=i[4]?i[4]:null,idGestionale:i.length>=6&&"null"!=i[5]&&"undefined"!=i[5]?i[5]:null,idPartizione:i.length>=7&&"null"!=i[6]&&"undefined"!=i[6]?i[6]:null}}),this.closeAfterResourceCreation&&("POPUP"==this.mode?(document.getElementById("tecaMainPopup").style.width="0%",document.getElementById("tecaMainPopup").outerHTML=""):this.mode&&"EMBEDDED"!=this.mode||(document.getElementById(this.tecaContainer).innerHTML=""))):"RESOURCE_UNLINKED"==n?this.tecaEventListener&&this.tecaEventListener({tipoEvento:n,payload:{uuidRisorsa:r,uuidRelazione:i[2],qualifica:"null"!=i[3]&&"undefined"!=i[3]?i[3]:null,idLogico:"null"!=i[4]&&"undefined"!=i[4]?i[4]:null,idConservatore:"null"!=i[5]&&"undefined"!=i[5]?i[5]:null,idGestionale:"null"!=i[6]&&"undefined"!=i[6]?i[6]:null,idPartizione:"null"!=i[7]&&"undefined"!=i[7]?i[7]:null}}):"SELECTED_MEDIA"!=n||(this.tecaEventListener&&this.tecaEventListener({tipoEvento:n,payload:{uuidMedia:r}}),"POPUP"==this.mode?(document.getElementById("tecaMainPopup").style.width="0%",document.getElementById("tecaMainPopup").outerHTML=""):this.mode&&"EMBEDDED"!=this.mode||(document.getElementById(this.tecaContainer).innerHTML=""))}}else console.log("No listener to receive teca event")},!1)};Teca.prototype.setEventListener=function(e){this.tecaEventListener=e},Teca.prototype.removeListener=function(){this.tecaEventListener=null},Teca.prototype.refreshAccessToken=function(e){this.currentToken=e,document.getElementById("teca").contentWindow.postMessage("REFRESH_TOKEN:-:"+e,"*")},Teca.prototype.registerOpenCallback=function(e){this.mode=e.mode,this.afterRefreshTokenCallback=function(t){var i=t||e;if(i.mode&&"EMBEDDED"!=i.mode){if("FULLPAGE"==i.mode){this.tecaContainer=i.tagEmbedded&&i.tagEmbedded.id?i.tagEmbedded.id:"teca-container";var n=document.createElement("iframe");n.id="teca",n.src=this.composeUrl(this.tecaEnv,i),n.setAttribute("scrolling","auto"),n.setAttribute("frameborder","0"),n.setAttribute("allow","encrypted-media *;"),n.margin="0";var r=document.getElementById(this.tecaContainer);r?r.appendChild(n):console.error("Impossible to find teca-container element to embed teca")}else if("POPUP"==i.mode){var _=this.composeUrl(this.tecaEnv,i);this.closeAfterResourceCreation=i.closeAfterResourceCreation;let l=null;if(document.getElementById("tecaMainPopup"))l=document.getElementById("tecaMainPopup").outerHTML="";else if("LINK_RESOURCE"==i.viewer)document.getElementById("tecaPopup")&&(document.getElementById("tecaPopup").outerHTML=""),this.buildLinkResourceButton(i);else{(l=document.createElement("div")).id="tecaMainPopup",l.style.height="100%",l.style.width="0",l.style.position="fixed",l.style.zIndex="2000",l.style.left="0",l.style.top="0",l.style.backgroundColor="rgb(0,0,0)",l.style.backgroundColor="rgba(0,0,0, 0.6)",l.style.overflowX="hidden",l.style.transition="0.5s";let o=document.createElement("div");o.style.position="absolute",o.style.top="5%",o.style.width="100%",o.style.textAlign="center";let a=document.createElement("a");a.style.position="absolute",a.style.top="5%",a.style.right="4.5%",a.style.fontSize="45px",a.style.color="#013366",a.style.cursor="pointer",a.style.width="50px",a.style.height="50px",a.style.border="2px solid #013366",a.style.borderRadius="15px",a.style.lineHeight="40px",a.innerText="x";var s=this;a.onclick=function(e){document.getElementById("tecaMainPopup").style.width="0%",document.getElementById("tecaMainPopup").outerHTML="",s.tecaEventListener?s.tecaEventListener({tipoEvento:"POPUP_CLOSED",payload:null}):console.log("No listener to receive teca event")},o.appendChild(a);var n=document.createElement("iframe");n.id="teca",n.src=_,n.height=window.innerHeight-100,n.width=window.innerWidth-100,n.setAttribute("scrolling","auto"),n.setAttribute("frameborder","0"),n.setAttribute("allow","encrypted-media *;"),o.appendChild(n),document.body.appendChild(l),l.appendChild(o),setTimeout(function(){document.getElementById("tecaMainPopup").style.width="100%"},200)}}}else if(document.getElementById("teca")&&(document.getElementById("teca").outerHTML=""),this.closeAfterResourceCreation=i.closeAfterResourceCreation,this.tecaContainer=i.tagEmbedded&&i.tagEmbedded.id?i.tagEmbedded.id:"teca-container","LINK_RESOURCE"==i.viewer)document.getElementById("tecaPopup")&&(document.getElementById("tecaPopup").outerHTML=""),this.buildLinkResourceButton(i);else{var _=this.composeUrl(this.tecaEnv,i),n=this.buildFrame(_,i),r=document.getElementById(this.tecaContainer);r?r.appendChild(n):console.error("Impossible to find teca-container element to embed teca")}}},Teca.prototype.buildLinkResourceButton=function(e){var t=this,i=document.createElement("span");i.id="closeButton",i.style.height="60px",i.style.width="60px",i.style.padding="20px",i.style.margin="15px",i.style.cursor="pointer",i.style.textAlign="end",i.style.display="block",i.onclick=function(e){document.getElementById("tecaPopup").style.width="0%",document.getElementById("tecaPopup").outerHTML=""};var n=document.createElement("div");n.id="searchButton",n.style.height="120px",n.style.width="90%",n.style.padding="20px",n.style.borderRadius="10px",n.style.border="2px solid #0266cc",n.style.justifyContent="center",n.style.alignItems="center",n.style.borderRadius="16px",n.style.display="flex",n.style.margin="5%",n.style.cursor="pointer",n.onclick=function(i){let n=JSON.parse(JSON.stringify(e));n.viewer="LINK_RESOURCE_LIST",t.afterRefreshTokenCallback(n),document.getElementById("tecaPopup").style.width="0%"};var r=document.createElement("div");r.id="searchButtonBox",r.style.height="120px",r.style.padding="20px",r.style.fontWeight="600",r.style.fontSize="30px",r.style.margin="23px 0 0 15px",r.style.color="#036",r.innerText="Cerca Risorsa";var _=document.createElement("div");_.id="searchButtonSvgBox",_.style.transform="scale(2.5)",_.style.color="#0266cc",_.style.paddingTop="7px",_.style.paddingLeft="15px",n.appendChild(_),n.appendChild(r);var l=document.createElement("div");l.id="searchButton",l.style.height="120px",l.style.width="90%",l.style.padding="20px",l.style.borderRadius="10px",l.style.border="2px solid #0266cc",l.style.justifyContent="center",l.style.alignItems="center",l.style.borderRadius="16px",l.style.display="flex",l.style.margin="5%",l.style.cursor="pointer",l.onclick=function(i){let n=JSON.parse(JSON.stringify(e));n.viewer="CREATE_RESOURCE",t.afterRefreshTokenCallback(n),document.getElementById("tecaPopup").style.width="0%"};var o=document.createElement("div");o.id="createButtonBox",o.style.height="120px",o.style.padding="20px",o.style.fontWeight="600",o.style.fontSize="30px",o.style.margin="23px 0 0 15px",o.style.color="#036",o.innerText="Crea Risorsa";var a=document.createElement("div");a.id="createButtonSvgBox",a.style.transform="scale(2.5)",a.style.color="#0266cc",a.style.paddingTop="7px",a.style.paddingLeft="15px",l.appendChild(a),l.appendChild(o);var s=document.createElement("div");s.id="linkChoiceWindow",s.style.width="650px",s.style.position="fixed",s.style.zIndex="2100",s.style.left="31%",s.style.backgroundColor="#FFFFFF",s.style.overflowX="hidden",s.style.transition="0.5s",s.style.borderRadius="10px",s.style.position="absolute",s.style.top="10%",s.style.textAlign="center";let $=document.createElement("div");$.id="tecaPopup",$.style.height="100%",$.style.width="100%",$.style.position="fixed",$.style.zIndex="2000",$.style.left="0",$.style.top="0",$.style.backgroundColor="rgb(0,0,0)",$.style.backgroundColor="rgba(0,0,0, 0.6)",$.style.overflowX="hidden",s.appendChild(i),s.appendChild(n),s.appendChild(l),$.appendChild(s),document.body.appendChild($);var d=document.getElementById("searchButtonSvgBox");d.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1" _ngcontent-ng-c3868550473="" aria-hidden="true"><g id="surface1" _ngcontent-ng-c3868550473=""><path style=" stroke:none;fill-rule:evenodd;fill:currentColor;fill-opacity:1;" d="M 16 15.300781 L 21.898438 21.101562 L 21.101562 21.898438 L 15.300781 16 C 13.824219 17.269531 11.945312 17.976562 10 18 C 5.582031 18 2 14.417969 2 10 C 2 5.582031 5.582031 2 10 2 C 14.417969 2 18 5.582031 18 10 C 17.976562 11.945312 17.269531 13.824219 16 15.300781 Z M 3 10 C 3 13.867188 6.132812 17 10 17 C 11.855469 17 13.636719 16.261719 14.949219 14.949219 C 16.261719 13.636719 17 11.855469 17 10 C 17 6.132812 13.867188 3 10 3 C 6.132812 3 3 6.132812 3 10 Z M 3 10 " _ngcontent-ng-c3868550473=""></path><path style=" stroke:none;fill-rule:nonzero;fill:currentColor;fill-opacity:1;" d="M 21.898438 21.101562 L 22.035156 21.234375 L 22.171875 21.097656 L 22.035156 20.964844 Z M 16 15.300781 L 15.855469 15.175781 L 15.738281 15.3125 L 15.867188 15.4375 Z M 21.101562 21.898438 L 20.964844 22.035156 L 21.097656 22.171875 L 21.234375 22.035156 Z M 15.300781 16 L 15.4375 15.867188 L 15.3125 15.738281 L 15.175781 15.855469 Z M 10 18 L 10 18.191406 L 10.003906 18.191406 Z M 18 10 L 18.191406 10.003906 L 18.191406 10 Z M 14.949219 14.949219 L 15.085938 15.085938 Z M 22.035156 20.964844 L 16.132812 15.164062 L 15.867188 15.4375 L 21.765625 21.234375 Z M 21.234375 22.035156 L 22.035156 21.234375 L 21.765625 20.964844 L 20.964844 21.765625 Z M 15.164062 16.132812 L 20.964844 22.035156 L 21.234375 21.765625 L 15.4375 15.867188 Z M 10.003906 18.191406 C 11.992188 18.164062 13.914062 17.441406 15.425781 16.144531 L 15.175781 15.855469 C 13.730469 17.09375 11.898438 17.785156 9.996094 17.808594 Z M 1.808594 10 C 1.808594 14.523438 5.476562 18.191406 10 18.191406 L 10 17.808594 C 5.6875 17.808594 2.191406 14.3125 2.191406 10 Z M 10 1.808594 C 5.476562 1.808594 1.808594 5.476562 1.808594 10 L 2.191406 10 C 2.191406 5.6875 5.6875 2.191406 10 2.191406 Z M 18.191406 10 C 18.191406 5.476562 14.523438 1.808594 10 1.808594 L 10 2.191406 C 14.3125 2.191406 17.808594 5.6875 17.808594 10 Z M 16.144531 15.425781 C 17.441406 13.914062 18.164062 11.992188 18.191406 10.003906 L 17.808594 9.996094 C 17.785156 11.898438 17.09375 13.730469 15.855469 15.175781 Z M 10 16.808594 C 6.238281 16.808594 3.191406 13.761719 3.191406 10 L 2.808594 10 C 2.808594 13.972656 6.027344 17.191406 10 17.191406 Z M 14.816406 14.816406 C 13.539062 16.09375 11.804688 16.808594 10 16.808594 L 10 17.191406 C 11.90625 17.191406 13.734375 16.433594 15.085938 15.085938 Z M 16.808594 10 C 16.808594 11.804688 16.09375 13.539062 14.816406 14.816406 L 15.085938 15.085938 C 16.433594 13.734375 17.191406 11.90625 17.191406 10 Z M 10 3.191406 C 13.761719 3.191406 16.808594 6.238281 16.808594 10 L 17.191406 10 C 17.191406 6.027344 13.972656 2.808594 10 2.808594 Z M 3.191406 10 C 3.191406 6.238281 6.238281 3.191406 10 3.191406 L 10 2.808594 C 6.027344 2.808594 2.808594 6.027344 2.808594 10 Z M 3.191406 10 " _ngcontent-ng-c3868550473=""></path></g></svg>';var d=document.getElementById("createButtonSvgBox");d.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" _ngcontent-ng-c3868550473="" aria-hidden="true"><path d="M20 12.5H12.5V20H11.5V12.5H4V11.5H11.5V4H12.5V11.5H20V12.5Z" fill="currentColor" _ngcontent-ng-c3868550473=""></path></svg>';var d=document.getElementById("closeButton");d.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" width="34" height="34" viewBox="0 0 35 35" fill="none" _ngcontent-ng-c3868550473="" aria-hidden="true"><circle cx="17" cy="17" r="17" fill="#0066CC" _ngcontent-ng-c3868550473=""></circle><path d="M17.7504 17.0002L24.9333 24.0759L24.0757 24.9336L17 17.7507L9.92431 24.9336L9.06665 24.0759L16.2495 17.0002L9.06665 9.92455L9.92431 9.06689L17 16.2498L24.0757 9.06689L24.9333 9.92455L17.7504 17.0002Z" fill="white" _ngcontent-ng-c3868550473=""></path></svg>'},Teca.prototype.buildFrame=function(e,t){var i=t.tagEmbedded&&t.tagEmbedded.height?t.tagEmbedded.height:"1000",n=t.tagEmbedded&&t.tagEmbedded.width?t.tagEmbedded.width:"1400",r=document.createElement("iframe");return r.id="teca",r.src=e,r.height=i,r.width=n,r.setAttribute("scrolling","auto"),r.setAttribute("frameborder","0"),r.setAttribute("allow","encrypted-media *;"),r},Teca.prototype.composeUrl=function(e,t){var i=e;return t.viewer?i+=this.getRoute(t.viewer,t.parameters):i+=this.getRoute("FULL",t.parameters),e==i?i+="/?env="+this.env+"&t="+this.currentToken:i+="&env="+this.env+"&t="+this.currentToken,i},Teca.prototype.getRoute=function(e,t){var i={logicalId:t.logicalId,conservativeId:t.conservativeId,managementId:t.managementId,dossierId:t.dossierId,conservativeIdType:t.conservativeIdType,qualifica:t.qualifica};if("CREATE_RESOURCE"==e){var n="#/resources/new-resource";return t&&(t.qualifica||t.logicalId||t.managementId||t.dossierId)?(n+="?clientFlow=Y",n+="&qualifica=",n+=t.qualifica):n+="?clientFlow=N",n+="&formParams="+encodeURIComponent(JSON.stringify(i)),n+="&src="+(t.simpleResourceCreation?t.simpleResourceCreation:"false")}if("LINK_RESOURCE_FULL"==e){var n="#/collega-record/";return t&&!t.rappresentazione?n+="0/":n+="1/",t&&!t.nativoDigitale?n+="0/":n+="1/",t&&!t.documentazione?n+="0/":n+="1/",n+="?clientFlow=Y",n+="&formParams="+encodeURIComponent(JSON.stringify(i)),t&&t.qualifica&&(n+="&qualifica="+t.qualifica),n+="&src="+(t.simpleResourceCreation?t.simpleResourceCreation:"false")}if("RESOURCE_DETAIL"==e&&t&&t.uuid){var n="#/public/resource-detail/";return n+=t.uuid+"/",n+="?clientFlow=N"}if("OPEN_VIEWER"==e&&t&&t.uuid&&t.uuidManifest){var n="#/public/viewer/";return n+=t.uuid+"/",n+=t.uuidManifest+"/",n+="?clientFlow=N"}if("OPEN_VIEWER"==e&&t&&t.manifestURI){var n="#/public/viewer";return n+="?clientFlow=N&manifestURI=",n+=encodeURIComponent(t.manifestURI)}else if("LINK_RESOURCE_LIST"==e){var n="#/collega-record-list/DIRECT";return n+="?clientFlow=Y",n+="&formParams="+encodeURIComponent(JSON.stringify(i)),t&&t.qualifica&&(n+="&qualifica="+t.qualifica),n+="&src="+(t.simpleResourceCreation?t.simpleResourceCreation:"false")}else if("LINK_CHILD_RESOURCE_LIST"==e){var n="#/collega-record-list/DIRECT";return n+="/"+t.parentDigitalResource,n+="?clientFlow=Y",n+="&formParams="+encodeURIComponent(JSON.stringify(i)),t&&t.qualifica&&(n+="&qualifica="+t.qualifica),n+="&src="+(t.simpleResourceCreation?t.simpleResourceCreation:"false")}else if("MEDIA_PICKER"==e){var n="#/public/resource-detail/";return n+=t.uuid+"/",n+="?clientFlow=N",n+="&pickerMediaMode=true"}else{var n="";return n+("?clientFlow=N&qualifica=undefined&formParams="+encodeURIComponent(JSON.stringify(i)))}},Teca.prototype.sendBackMandatoryError=function(e){this.tecaEventListener({tipoEvento:"MISSING_MANDATORY_FIELDS",payload:{fields:e}})},Teca.prototype.open=function(e){var t=this.checkMandatoryFields(e.viewer,e.parameters);t?this.sendBackMandatoryError(t):(this.registerOpenCallback(e),this.refreshAccessTokenInternal())},Teca.prototype.checkMandatoryFields=function(e,t){var i=null;return"LINK_CHILD_RESOURCE_LIST"!=e||t.parentDigitalResource||(i=["parentDigitalResource"]),i},Teca.prototype.refreshAccessTokenInternal=function(){this.currentTokenUrl?this.getToken():this.tecaEventListener?this.tecaEventListener.notify(new RefreshTokenEvent("REFRESH_TOKEN",{access_token:this.currentToken})):alert("Error: url or token listener not present")},Teca.prototype.getToken=function(){let e=new XMLHttpRequest;e.open("POST",this.currentTokenUrl,!0),e.setRequestHeader("Content-Type","application/json;charset=UTF-8");var t=this;e.onreadystatechange=function(){if(4==this.readyState){if(200===this.status){var e=JSON.parse(this.responseText);t.currentToken=e.access_token,t.afterRefreshTokenCallback()}else console.error("Error: Impossible to get token")}},e.send()},Teca.prototype.close=function(){"POPUP"==this.mode?(document.getElementById("tecaMainPopup").style.width="0%",document.getElementById("tecaMainPopup").outerHTML=""):this.mode&&"EMBEDDED"!=this.mode||(document.getElementById(this.tecaContainer).innerHTML="")};
