'use strict';

/**
 * Build svg string with needed features like accessibility and custom attributes in svg element.
 */
const createSvg = (params = {}) => {
  const { content = '', title, attributes = [] } = params;
  let titleElement = '';
  let attributeCollection = [
    { name: 'xmlns', value: 'http://www.w3.org/2000/svg' },
    { name: 'fill', value: 'currentColor' },
    ...attributes,
  ];
  // If title is defined, add title functionality.
  if (title) {
    titleElement = `<title>${title}</title>`;
  }
  // Create string from array of attributes
  const attributesString = attributeCollection
    .map(item => `${item.name}="${item.value}"`)
    .join(' ');
  return `<svg ${attributesString}>${titleElement}${content}</svg>`;
};

/**
 * Return content of svg element of full svg component string
 */
const getSvgContent = (svgString) => {
  // Remove svg tags
  return svgString.replace(/<(\/?)svg([^>]*)>/g, '').trim();
};

exports.createSvg = createSvg;
exports.getSvgContent = getSvgContent;
