/**
 * External dependencies
 */
import DOMPurify from "dompurify";

/**
 * WordPress dependencies
 */
import { cleanForSlug } from "@wordpress/url";

/**
 * Internal dependencies
 */

/**
 * Sanitize slug
 */
export { cleanForSlug as sanitizeSlug };

/**
 * Sanitize raw SVG string
 *
 * @param {String} dirty
 * @returns {String}
 */
export const sanitizeSVG = (dirty) => {
  const sanitizedSVG = DOMPurify.sanitize(dirty, {
    USE_PROFILES: { svg: true, svgFilters: true },
  });

  if (!sanitizedSVG) {
    return sanitizedSVG;
  }

  // Sanitize it again to reverse the order as original because dompurify reverse the order of properties
  // Maybe we will remove this snippet for the sake of the stupid idea
  return DOMPurify.sanitize(sanitizedSVG, {
    USE_PROFILES: { svg: true, svgFilters: true },
  });
};

/**
 * Remove HTML tag from string
 */
export const sanitizeHTMLTag = (dirty) => {
  return dirty.replace(/(<([^>]+)>)/gi, "");
};
