{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["\r\nimport { getDocument } from \"ssr-window\";\r\n\r\nexport type TGetElWrapperArgs = Parameters<typeof getElWrapper>;\r\n\r\nexport type TGetElWrapperReturn = ReturnType<typeof getElWrapper>;\r\n\r\n/**\r\n * Gets a wrapper for specific element\r\n * @param el{HTMLElement} DOM element\r\n * @param str{string} string of wrapper HTML layout (supports nested blocks)\r\n * @returns {HTMLElement}\r\n * @throws {TypeError} getElWrapper: el must be an HTMLElement\n * @throws {TypeError} getElWrapper: str must be a non-empty string\n * @throws {TypeError} getElWrapper: str must contain an HTML element\n * @example\r\n * // How to wrap content to the few nested `div` blocks?\r\n * // <div id=\"block\">My Element</div>\r\n * const wrapperLayout = `\r\n *  <div class=\"wrapper\">\r\n *    <div class=\"wrapper__inner\"></div>\r\n *  </div>\r\n * `;\r\n * const el = document.getElementById(\"block\");\r\n * const wrapped = getElWrapper(el, wrapperLayout);\n * console.log(wrapped.outerHTML); // => `<div class=\"wrapper\"><div class=\"wrapper__inner\"><div id=\"block\">My Element</div></div></div>`\n * @example\n * // Wrap a form field with reusable validation markup\n * const fieldWrapper = getElWrapper(input, `\n *   <label class=\"field\"><span class=\"field__control\"></span></label>\n * `);\n */\nexport const getElWrapper = (el: HTMLElement, str: string): HTMLElement => {\r\n  if (!el || typeof (el as any).nodeType !== \"number\") {\r\n    throw new TypeError(\"getElWrapper: el must be an HTMLElement\");\r\n  }\r\n  if (typeof str !== \"string\" || str.length === 0) {\r\n    throw new TypeError(\"getElWrapper: str must be a non-empty string\");\r\n  }\r\n  const temp = getDocument().createElement(\"div\");\n  const parent = el.parentNode;\n  const nextSibling = el.nextSibling;\n  temp.innerHTML = str.trim();\n  const wrapper = temp.firstElementChild as HTMLElement | null;\n  if (!wrapper) {\n    throw new TypeError(\"getElWrapper: str must contain an HTML element\");\n  }\n  let target = wrapper;\n  while (target.firstElementChild) {\n    target = target.firstElementChild as HTMLElement;\n  }\n  target.appendChild(el);\n  if (parent) {\n    parent.insertBefore(wrapper, nextSibling);\n  }\n  return wrapper;\n};\n"],"names":["getElWrapper","el","str","nodeType","TypeError","length","temp","getDocument","createElement","parent","parentNode","nextSibling","innerHTML","trim","wrapper","firstElementChild","target","appendChild","insertBefore"],"mappings":"0CAgCaA,aAAe,CAACC,GAAiBC,OAC5C,IAAKD,WAAcA,GAAWE,WAAa,SACzC,MAAM,IAAIC,UAAU,2CAEtB,UAAWF,MAAQ,UAAYA,IAAIG,SAAW,EAC5C,MAAM,IAAID,UAAU,gDAEtB,MAAME,KAAOC,cAAcC,cAAc,OACzC,MAAMC,OAASR,GAAGS,WAClB,MAAMC,YAAcV,GAAGU,YACvBL,KAAKM,UAAYV,IAAIW,OACrB,MAAMC,QAAUR,KAAKS,kBACrB,IAAKD,QACH,MAAM,IAAIV,UAAU,kDAEtB,IAAIY,OAASF,QACb,MAAOE,OAAOD,kBACZC,OAASA,OAAOD,kBAElBC,OAAOC,YAAYhB,IACnB,GAAIQ,OACFA,OAAOS,aAAaJ,QAASH,aAE/B,OAAOG"}