"use strict";

exports.__esModule = true;
exports.useImageFit = useImageFit;
exports.default = void 0;

var _react = require("react");

var _reactUse = require("react-use");

function useImageFit(existingImageRef) {
  const [divRef, divDimensions] = (0, _reactUse.useMeasure)();
  const ref = (0, _react.useRef)(); // gotta create it, can't be conditional

  const imageRef = existingImageRef || ref;
  const img = imageRef == null ? void 0 : imageRef.current;

  if (img) {
    const {
      naturalHeight,
      naturalWidth,
      style
    } = img;
    const {
      width,
      height
    } = divDimensions || {};
    style.width = 'auto';
    style.height = 'auto';
    const imgAspect = naturalWidth / naturalHeight;
    const divAspect = width / height;

    if (imgAspect > divAspect) {
      style.width = '100%';
    } else {
      style.height = '100%';
    }
  }

  return [divRef, divDimensions, imageRef];
}

var _default = useImageFit;
exports.default = _default;