import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["src", "alt", "fallback", "width", "height", "onError", "style", "className", "prefixCls", "previewPrefixCls"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import React, { useEffect } from 'react';
import { useState, useRef } from 'react';
import cn from 'classnames';
import Preview from './Preview';
import PreviewGroup, { context } from './PreviewGroup';
import { icons, preview } from './settings.js';
var uuid = 0;

var ImageInternal = function ImageInternal(_ref) {
  var imgSrc = _ref.src,
      alt = _ref.alt,
      fallback = _ref.fallback,
      width = _ref.width,
      height = _ref.height,
      onImageError = _ref.onError,
      style = _ref.style,
      className = _ref.className,
      _ref$prefixCls = _ref.prefixCls,
      prefixCls = _ref$prefixCls === void 0 ? 'ne-image' : _ref$prefixCls,
      _ref$previewPrefixCls = _ref.previewPrefixCls,
      previewPrefixCls = _ref$previewPrefixCls === void 0 ? "".concat(prefixCls, "-preview") : _ref$previewPrefixCls,
      otherProps = _objectWithoutProperties(_ref, _excluded);

  var _React$useContext = React.useContext(context),
      isPreviewGroup = _React$useContext.isPreviewGroup,
      registerImage = _React$useContext.registerImage,
      setCurrent = _React$useContext.setCurrent,
      setGroupShowPreview = _React$useContext.setShowPreview;

  var _useState = useState(function () {
    return uuid++;
  }),
      _useState2 = _slicedToArray(_useState, 1),
      currentId = _useState2[0];

  var previewMask = preview.previewMask,
      icons = preview.icons,
      previewSrc = preview.previewSrc;
  var src = previewSrc !== null && previewSrc !== void 0 ? previewSrc : imgSrc; //previewSrc这里是undefined呵呵

  var _useState3 = useState('normal'),
      _useState4 = _slicedToArray(_useState3, 2),
      status = _useState4[0],
      setStatus = _useState4[1];

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      isShowPreview = _useState6[0],
      setShowPreview = _useState6[1];

  var isError = status === 'error';
  var canPreview = !isError;
  var isLoaded = useRef(false);

  var onLoad = function onLoad() {
    setStatus('normal');
  };

  var onError = function onError(e) {
    if (onImageError) {
      onImageError(e);
    }

    setStatus('error');
  };

  var onPreview = function onPreview() {
    if (isPreviewGroup) {
      setCurrent(currentId);
      setGroupShowPreview(true);
    } else {
      setShowPreview(true);
    }
  };

  var onPreviewClose = function onPreviewClose(e) {
    e.stopPropagation();
    setShowPreview(false);
  };

  var getImgRef = function getImgRef() {};

  var imgCommonProps = {
    alt: alt,
    className: cn("".concat(prefixCls, "-img"), className),
    style: _objectSpread({
      width: width,
      height: height
    }, style)
  };
  var wrapperClass = cn(prefixCls, _defineProperty({}, "".concat(wrapperClass, "-error"), isError));
  var mergedSrc = isError && fallback ? fallback : src;
  useEffect(function () {
    var unregister = registerImage(currentId, src);
    return unregister;
  }, []); // Keep order start

  useEffect(function () {
    registerImage(currentId, src, canPreview);
  }, [src, canPreview]); // Keep order end

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: wrapperClass,
    style: {
      width: width,
      height: height
    },
    onClick: preview && !isError ? onPreview : function () {}
  }, /*#__PURE__*/React.createElement("img", _extends({}, imgCommonProps, {
    ref: getImgRef
  }, isError && fallback ? {
    src: fallback
  } : {
    onLoad: onLoad,
    onError: onError,
    src: imgSrc
  })), !isPreviewGroup && canPreview && /*#__PURE__*/React.createElement(Preview, {
    prefixCls: previewPrefixCls,
    visible: isShowPreview,
    icons: icons,
    onClose: onPreviewClose,
    src: mergedSrc
  })));
};

ImageInternal.PreviewGroup = PreviewGroup;
export default ImageInternal;