import { dashCaseToCamel } from '../../../utils/string';
/**
 * Creates a style map from the inline styles of an HTML element. This function extracts the 'style' attribute from the given element.
 *
 * @param {HTMLElement} element The HTML element from which to extract inline styles.
 * @returns {AnnotationStyle} An object representing the custom style annotations.
 */
export function createStyleMapFromElement(element) {
  const customStyle = element.getAttribute('style');
  if (!customStyle)
    return {};
  const styleValue = customStyle.split(';').filter(Boolean);
  return styleValue
    .filter((key) => key !== ' ')
    .map((key) => {
    // to remove redundant quote pairs if needed, for example: ['fontFamily: "Roboto Mono"'] will be updated to ['fontFamily: Roboto Mono']
    const styleObject = key.replace(/(['"])(.*?)\1/g, '$2').split(':');
    const styleKey = dashCaseToCamel(styleObject[0].trim());
    return {
      [styleKey]: styleObject[1].trim().replace(';', ''),
    };
  })
    .reduce((current, obj) => (Object.assign(Object.assign({}, current), obj)), {});
}
