(function () {
  'use strict';

  function syncThumbnailAspectRatio() {
    var frames = document.querySelectorAll('.sitsel-post-thumbnail-frame');

    frames.forEach(function (frame) {
      if (frame.style.getPropertyValue('--sitsel-post-thumbnail-aspect-ratio')) {
        return;
      }

      var img = frame.querySelector('.sitsel-post-thumbnail-image');
      if (!img) {
        return;
      }

      var width = parseInt(img.getAttribute('width') || '', 10);
      var height = parseInt(img.getAttribute('height') || '', 10);

      if (width > 0 && height > 0) {
        frame.style.setProperty('--sitsel-post-thumbnail-aspect-ratio', width + ' / ' + height);
      }
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', syncThumbnailAspectRatio, { once: true });
  } else {
    syncThumbnailAspectRatio();
  }
})();
