"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.PictureProvider = void 0;

var _react = _interopRequireWildcard(require("react"));

var _preview = _interopRequireDefault(require("../modal/preview"));

var PictureContext = /*#__PURE__*/(0, _react.createContext)(undefined);
var _default = PictureContext;
exports["default"] = _default;

var PictureProvider = function PictureProvider(props) {
  var children = props.children,
      contextModalProps = props.modalProps;
  var pictureList = (0, _react.useRef)([]);
  var registerPicture = (0, _react.useCallback)(function (index, pictureRef) {
    if (pictureList.current[index] !== pictureRef) {
      pictureList.current[index] = pictureRef;
    }
  }, [pictureList]);
  var unRegisterPicture = (0, _react.useCallback)(function (index, pictureRef) {
    if (pictureList.current[index] === pictureRef) {
      pictureList.current[index] = undefined;
    }
  }, [pictureList]);
  var preview = (0, _react.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;
      });
    }

    (0, _preview["default"])({
      defaultIndex: defaultIndex,
      list: list
    }, modalProps);
  }, [pictureList]);
  var value = (0, _react.useMemo)(function () {
    return {
      registerPicture: registerPicture,
      unRegisterPicture: unRegisterPicture,
      preview: preview
    };
  }, [registerPicture, unRegisterPicture, preview]);
  return /*#__PURE__*/_react["default"].createElement(PictureContext.Provider, {
    value: value
  }, children);
};

exports.PictureProvider = PictureProvider;
//# sourceMappingURL=PictureContext.js.map
