import mapsheets from './assets/images/mapsheets.svg';

const doc = new DOMParser().parseFromString(mapsheets, 'image/svg+xml');
const refs = [];

document.querySelectorAll('use[href*=".svg"').forEach(el => {
  const fragId = el.getAttributeNS(null, 'href').split('.svg').pop();
  const frag = doc.querySelector(fragId);
  frag.querySelectorAll('use').forEach(el => refs.push(el.getAttributeNS(null, 'href')));
  if (el.style.transform) frag.style.transform = el.style.transform;
  el.replaceWith(frag);
});

const refsQuery = [...new Set([...refs])].join(', ');
const refNodes = doc.querySelectorAll(refsQuery);
const defs = document.querySelector('defs');

refNodes.forEach(n => defs.appendChild(n));
