{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["import { getDocument } from \"ssr-window\";\r\n\r\ntype TGetExternalScriptProps = {\n  isAsync?: boolean;\n  isDefer?: boolean;\n  src: string;\n  appendTo?: Node | HTMLElement;\n  id?: string;\n  crossorigin?: string;\n  integrity?: string;\n  type?: string;\n};\n\r\nexport type TGetExternalScriptArgs = Parameters<typeof getExternalScript>;\r\n\r\nexport type TGetExternalScriptReturn = ReturnType<typeof getExternalScript>;\r\n\r\n/**\r\n * Appends an external script to the page and resolves when it's loaded.\r\n *\r\n * @param {TGetExternalScriptProps} props Options\r\n * @param {boolean} [props.isAsync=true] `async` attribute\r\n * @param {boolean} [props.isDefer=false] `defer` attribute\r\n * @param {string} props.src Script source URL\r\n * @param {Node|HTMLElement} [props.appendTo=document.body] Element to append the script to\n * @param {string} [props.id] Script element id\n * @param {string} [props.crossorigin] `crossorigin` attribute\n * @param {string} [props.integrity] `integrity` attribute\n * @param {string} [props.type] `type` attribute\n * @returns {Promise<HTMLScriptElement>} Promise that resolves to the created script element\n * @example\r\n * getExternalScript({ src: \"https://cdn.example.com/lib.js\", id: \"lib\" })\r\n *   .then(() => console.log(\"Loaded\"));\r\n */\r\nexport const getExternalScript = (props: TGetExternalScriptProps): Promise<HTMLScriptElement> => {\r\n  const {\r\n    isAsync = true,\r\n    isDefer = false,\r\n    src,\r\n    type,\r\n    appendTo = getDocument().body,\n    id,\n    crossorigin,\n    integrity,\n  } = props ?? ({} as TGetExternalScriptProps);\n\r\n  if (typeof src !== \"string\" || !src) {\r\n    return Promise.reject(new TypeError(\"getExternalScript: props.src must be a non-empty string\"));\r\n  }\r\n  if (!!crossorigin && typeof crossorigin !== \"string\") {\n    return Promise.reject(new TypeError(\"getExternalScript: props.crossorigin must be a string if provided\"));\n  }\n  if (!!integrity && typeof integrity !== \"string\") {\n    return Promise.reject(new TypeError(\"getExternalScript: props.integrity must be a string if provided\"));\n  }\n  if (!appendTo || typeof (appendTo as any).appendChild !== \"function\") {\n    return Promise.reject(new TypeError(\"getExternalScript: props.appendTo must be a Node/HTMLElement if provided\"));\n  }\n\r\n  return new Promise<HTMLScriptElement>((resolve, reject) => {\r\n    let isReady = false;\r\n    const script = getDocument().createElement(\"script\");\r\n    script.src = src;\r\n    script.async = isAsync;\r\n    script.defer = isDefer;\r\n\r\n    if (id) {\r\n      script.id = id;\r\n    }\r\n    if (crossorigin) {\n      (script as any).crossorigin = crossorigin;\n    }\n    if (integrity) {\n      script.integrity = integrity;\n    }\n    if (type) {\n      script.type = type;\n    }\n\r\n    script.onerror = (err: Event | string) => {\r\n      reject(err);\r\n      script.onerror = null;\r\n    };\r\n    script.onload = script.onreadystatechange = function (this: any): void {\r\n      if (!isReady && (!this.readyState || this.readyState === \"complete\")) {\r\n        isReady = true;\r\n        resolve(script);\r\n        script.onload = null;\r\n        script.onerror = null;\r\n      }\r\n    };\r\n\r\n    appendTo.appendChild(script);\r\n  });\r\n};\r\n"],"names":["getExternalScript","props","isAsync","isDefer","src","type","appendTo","getDocument","body","id","crossorigin","integrity","Promise","reject","TypeError","appendChild","resolve","isReady","script","createElement","async","defer","onerror","err","onload","onreadystatechange","this","readyState"],"mappings":";;;;;;;;;;;;;;;;;GAkCO,MAAMA,kBAAqBC,QAChC,MAAMC,QACJA,QAAU,KAAIC,QACdA,QAAU,MAAKC,IACfA,IAAGC,KACHA,KAAIC,SACJA,SAAWC,UAAAA,cAAcC,KAAIC,GAC7BA,GAAEC,YACFA,YAAWC,UACXA,WACEV,OAAU,CAAA,EAEd,UAAWG,MAAQ,WAAaA,IAC9B,OAAOQ,QAAQC,OAAO,IAAIC,UAAU,4DAEtC,KAAMJ,oBAAsBA,cAAgB,SAC1C,OAAOE,QAAQC,OAAO,IAAIC,UAAU,sEAEtC,KAAMH,kBAAoBA,YAAc,SACtC,OAAOC,QAAQC,OAAO,IAAIC,UAAU,oEAEtC,IAAKR,iBAAoBA,SAAiBS,cAAgB,WACxD,OAAOH,QAAQC,OAAO,IAAIC,UAAU,6EAGtC,OAAO,IAAIF,QAA2B,CAACI,QAASH,UAC9C,IAAII,QAAU,MACd,MAAMC,OAASX,UAAAA,cAAcY,cAAc,UAC3CD,OAAOd,IAAMA,IACbc,OAAOE,MAAQlB,QACfgB,OAAOG,MAAQlB,QAEf,GAAIM,GACFS,OAAOT,GAAKA,GAEd,GAAIC,YACDQ,OAAeR,YAAcA,YAEhC,GAAIC,UACFO,OAAOP,UAAYA,UAErB,GAAIN,KACFa,OAAOb,KAAOA,KAGhBa,OAAOI,QAAWC,MAChBV,OAAOU,KACPL,OAAOI,QAAU,MAEnBJ,OAAOM,OAASN,OAAOO,mBAAqB,WAC1C,IAAKR,WAAaS,KAAKC,YAAcD,KAAKC,aAAe,YAAa,CACpEV,QAAU,KACVD,QAAQE,QACRA,OAAOM,OAAS,KAChBN,OAAOI,QAAU,IACnB,CACF,EAEAhB,SAASS,YAAYG"}