{"version":3,"file":"index-fehEXX9L.cjs","names":["toArray","value","camelCaseToKebabCase","kebab","i","str","char","lowercase","mount","container","to","el","setAttrs","attr","attrs","clearAttrs","setAttr","name","createStyle","id","css"],"sources":["../../../common-utils/src/misc.ts","../../../common-utils/src/dom.ts"],"sourcesContent":["import type { MaybeArray } from \"@/types\";\n\n/**\n * Converts value to an array:\n *\n * 1. If value is already an array, returns it as-is.\n * 1. Otherwise wraps it in array: `[value]`\n *\n * @example\n *\n * toArray([\"apples\", \"oranges\"]); // [\"apples\", \"oranges\"]\n * toArray([\"apples\"]); // [\"apples\"]\n * toArray(\"apples\"); // [\"apples\"]\n *\n * @param value Value\n * @returns Value as an array\n */\nexport function toArray<T>(value: MaybeArray<T>) {\n  return Array.isArray(value) ? value : [value];\n}\n\n/**\n * Transforms string in `camelCase` or `PascalCase` into `kebab-case`\n * @param str String in `camelCase`\n * @returns String in `kebab-case`\n */\nexport function camelCaseToKebabCase(str: string) {\n  let kebab = \"\";\n\n  for (let i = 0; i < str.length; i++) {\n    const char = str[i];\n    const lowercase = char.toLowerCase();\n\n    if (i !== 0 && char !== lowercase) {\n      kebab += \"-\";\n    }\n\n    kebab += lowercase;\n  }\n\n  return kebab;\n}\n","/**\n * HTML element or selector that will be passed to `document.querySelector()`\n */\nexport type ElementOrSelector = Element | string;\n\n/**\n *\n */\nexport type SettableAttributeValue = string | null | undefined;\n\n/**\n * Appends given element to the given container\n * @param el Element to append\n * @param to Container\n * @returns Container\n */\nexport function mount(el: Element, to: ElementOrSelector) {\n  const container = typeof to === \"string\" ? document.querySelector(to) : to;\n\n  if (!container) {\n    console.error(`No elements found for selector \"${to}\"`);\n    return;\n  }\n\n  container.appendChild(el);\n  return container;\n}\n\n/**\n * Sets attributes of an element. Ignores `id`, `class` and `style` attributes.\n *\n * @param el Element to set attributes of\n * @param attrs Attributes object\n * @param clear If `true`, all element attributes will be cleared\n */\nexport function setAttrs(el: Element, attrs: Record<string, SettableAttributeValue>) {\n  for (const attr in attrs) {\n    setAttr(el, attr, attrs[attr]);\n  }\n}\n\n/**\n * Clears attributes of an element.\n *\n * @param el Element whose attributes must be cleared\n */\nexport function clearAttrs(el: Element) {\n  // Copy attrs because el.removeAttribute() will break iteration order\n  const attrs: string[] = [];\n\n  for (const attr of el.attributes) {\n    attrs.push(attr.name);\n  }\n\n  for (const attr of attrs) {\n    el.removeAttribute(attr);\n  }\n}\n\n/**\n * Sets an attribute of an element\n *\n * @param el Element\n * @param name Attribute name\n * @param value Attribute value\n */\nexport function setAttr(el: Element, name: string, value: SettableAttributeValue) {\n  if (typeof value === \"string\") {\n    el.setAttribute(name, value);\n  } else {\n    el.removeAttribute(name);\n  }\n}\n\n/**\n * Creates and appends to the `<head>` a unique style element. Behavior:\n *\n * 1. If there's no element with the given ID, will create a style element.\n * 1. If existing element **is** a style, won't do anything.\n * 1. If existing element **is not** a style, it will be replaced with a style element\n *\n * @param id Style ID\n * @param css CSS styles\n * @returns Created style element\n */\nexport function createStyle(id: string, css: string) {\n  let el = document.getElementById(id);\n\n  if (el) {\n    if (el instanceof HTMLStyleElement) {\n      return el;\n    }\n\n    el.remove();\n  }\n\n  el = document.createElement(\"style\");\n  el.innerHTML = css;\n  el.id = id;\n  document.head.appendChild(el);\n\n  return el;\n}\n"],"mappings":"AAiBA,SAAgBA,EAAW,EAAsB,CAC/C,OAAO,MAAM,QAAQC,CAAK,EAAIA,EAAQ,CAACA,CAAK,CAC9C,CAOA,SAAgBC,EAAqB,EAAa,CAChD,IAAIC,EAAQ,GAEZ,IAAK,IAAIC,EAAI,EAAGA,EAAIC,EAAI,OAAQ,IAAK,CACnC,IAAMC,EAAOD,EAAID,GACXG,EAAYD,EAAK,YAAY,EAMnC,IAJU,GAAKA,IAASC,IACtB,GAAS,KAGX,GAASA,CACX,CAEA,OAAOJ,CACT,CCzBA,SAAgBK,EAAM,EAAa,EAAuB,CACxD,IAAMC,EAAY,OAAOC,GAAO,SAAW,SAAS,cAAcA,CAAE,EAAIA,EAExE,GA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