"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Image", {
    enumerable: true,
    get: function() {
        return Image;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _taro = /*#__PURE__*/ _interop_require_default._(require("@tarojs/taro"));
var _components = require("@tarojs/components");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _pxtransform = require("../../utils/taro/px-transform");
var defaultProps = {
    src: '',
    error: true,
    loading: true
};
var Image = function Image(props) {
    var classPrefix = 'nut-image';
    var _ref = (0, _object_spread._)({}, defaultProps, props), className = _ref.className, style = _ref.style, src = _ref.src, width = _ref.width, height = _ref.height, radius = _ref.radius, error = _ref.error, loading = _ref.loading, onLoad = _ref.onLoad, onError = _ref.onError, rest = (0, _object_without_properties._)(_ref, [
        "className",
        "style",
        "src",
        "width",
        "height",
        "radius",
        "error",
        "loading",
        "onLoad",
        "onError"
    ]);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(true), 2), innerLoading = _useState[0], setInnerLoading = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), isError = _useState1[0], setIsError = _useState1[1];
    var pxCheck = function pxCheck(value) {
        return Number.isNaN(Number(value)) ? String(value) : (0, _pxtransform.pxTransform)(+value);
    };
    // 图片加载
    var handleLoad = function handleLoad(e) {
        setIsError(false);
        setInnerLoading(false);
        onLoad && onLoad(e);
    };
    var handleError = function handleError(e) {
        if (src) {
            setIsError(true);
            setInnerLoading(false);
            onError && onError(e);
        }
    };
    var containerStyle = (0, _object_spread._)({}, height ? {
        height: pxCheck(height)
    } : {}, width ? {
        width: pxCheck(width)
    } : {}, radius !== undefined && radius !== null ? {
        overflow: 'hidden',
        borderRadius: pxCheck(radius)
    } : {}, style);
    var imgStyle = (0, _object_spread_props._)((0, _object_spread._)({}, style), {
        width: width,
        height: height
    });
    var renderErrorImg = (0, _react.useCallback)(function() {
        if (!isError) return null;
        if (typeof error === 'boolean' && error === true && !innerLoading) {
            return /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-error")
            }, /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.ImageError, null));
        }
        if (/*#__PURE__*/ _react.default.isValidElement(error) && !innerLoading) {
            return /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-error")
            }, error);
        }
        return null;
    }, [
        error,
        isError,
        innerLoading
    ]);
    var renderLoading = (0, _react.useCallback)(function() {
        if (!loading) return null;
        if (typeof loading === 'boolean' && loading === true && innerLoading) {
            return /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-loading")
            }, /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Image, null));
        }
        if (/*#__PURE__*/ _react.default.isValidElement(loading) && innerLoading) {
            return /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-loading")
            }, loading);
        }
        return null;
    }, [
        loading,
        innerLoading
    ]);
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)(classPrefix, (0, _define_property._)({}, "".concat(classPrefix, "-basic"), _taro.default.getEnv() !== 'WEB'), className),
        style: containerStyle
    }, /*#__PURE__*/ _react.default.createElement(_components.Image, (0, _object_spread_props._)((0, _object_spread._)({}, rest), {
        className: (0, _classnames.default)("".concat(classPrefix, "-default"), className && "".concat(className, "-image"), (0, _define_property._)({}, "".concat(classPrefix, "-error"), isError)),
        style: imgStyle,
        src: src,
        onLoad: function onLoad(e) {
            return handleLoad(e);
        },
        onError: function onError(e) {
            return handleError(e);
        }
    })), /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, renderLoading(), renderErrorImg()));
};
Image.displayName = 'NutImage';
