export const getMediaAttributes = (dom: HTMLElement) => {
  const child = dom.querySelector('img, iframe, video');
  if (!child) return null;

  const findWidthInElement = (element: HTMLElement): string | null => {
    const width = element.style.width;
    if (width && width !== 'auto') return width;

    for (const child of element.children) {
      const childWidth = findWidthInElement(child as HTMLElement);
      if (childWidth) return childWidth;
    }

    return null;
  };

  const width = findWidthInElement(dom) || 'auto';
  const textAlign = dom.style.textAlign || 'inherit';
  const tag = child.tagName.toLowerCase();
  const src =
    child.getAttribute('src') ||
    child.querySelector('source')?.getAttribute('src') ||
    null;
  const srcdoc = child.getAttribute('srcdoc');

  return {
    src,
    srcdoc,
    tag,
    width,
    textAlign,
  };
};
