import React, { createContext, useCallback, useMemo, useRef } from 'react';
import modalPreview from '../modal/preview';
var PictureContext = /*#__PURE__*/createContext(undefined);
export default PictureContext;
export var PictureProvider = function PictureProvider(props) {
  var children = props.children,
      contextModalProps = props.modalProps;
  var pictureList = useRef([]);
  var registerPicture = useCallback(function (index, pictureRef) {
    if (pictureList.current[index] !== pictureRef) {
      pictureList.current[index] = pictureRef;
    }
  }, [pictureList]);
  var unRegisterPicture = useCallback(function (index, pictureRef) {
    if (pictureList.current[index] === pictureRef) {
      pictureList.current[index] = undefined;
    }
  }, [pictureList]);
  var preview = useCallback(function (index) {
    var modalProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : contextModalProps;
    var originalList = pictureList.current;
    var list = originalList.filter(function (picture) {
      return picture && picture.src;
    });
    var defaultIndex = index;

    if (list.length !== originalList.length) {
      var _originalList$index;

      var currentImgSrc = (_originalList$index = originalList[index]) === null || _originalList$index === void 0 ? void 0 : _originalList$index.src;
      defaultIndex = list.findIndex(function (_ref) {
        var src = _ref.src;
        return src === currentImgSrc;
      });
    }

    modalPreview({
      defaultIndex: defaultIndex,
      list: list
    }, modalProps);
  }, [pictureList]);
  var value = useMemo(function () {
    return {
      registerPicture: registerPicture,
      unRegisterPicture: unRegisterPicture,
      preview: preview
    };
  }, [registerPicture, unRegisterPicture, preview]);
  return /*#__PURE__*/React.createElement(PictureContext.Provider, {
    value: value
  }, children);
};
//# sourceMappingURL=PictureContext.js.map
