"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.ImageDecorator = exports.ImageBase = exports.Image = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _hoc = _interopRequireDefault(require("@enact/core/hoc"));
var _Image = _interopRequireDefault(require("@enact/ui/Image"));
var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));
var _resolution = require("@enact/ui/resolution");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _compose = _interopRequireDefault(require("ramda/src/compose"));
var _react = require("react");
var _Skinnable = _interopRequireDefault(require("../Skinnable"));
var _ImageModule = _interopRequireDefault(require("./Image.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["css"];
/**
 * Provides agate styled Image component that supports multiple resolution sources.
 *
 * @example
 * <Image src="https://dummyimage.com/64/e048e0/0011ff" style={{height: 64, width: 64}} />
 *
 * @module agate/Image
 * @exports Image
 * @exports ImageBase
 * @exports ImageDecorator
 */
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var _placeholder = 'data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC' + '9zdmciPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIHN0cm9rZT0iIzU1NSIgZmlsbD0iI2FhYSIg' + 'ZmlsbC1vcGFjaXR5PSIwLjIiIHN0cm9rZS1vcGFjaXR5PSIwLjgiIHN0cm9rZS13aWR0aD0iNiIgLz48L3N2Zz' + '4NCg==';

/**
 * An Agate-styled image component without any behavior
 *
 * @class ImageBase
 * @memberof agate/Image
 * @extends ui/Image.Image
 * @ui
 * @public
 */
var ImageBase = exports.ImageBase = (0, _kind["default"])({
  name: 'Image',
  propTypes: /** @lends agate/Image.ImageBase.prototype */{
    /**
     * Customizes the component by mapping the supplied collection of CSS class names to the
     * corresponding internal elements and states of this component.
     *
     * The following classes are supported:
     *
     * * `image` - The root component class for Image
     *
     * @type {Object}
     * @public
     */
    css: _propTypes["default"].object,
    /**
     * String value or Object of values used to determine which image will appear on
     * a specific screenSize.
     *
     * @type {String|Object}
     * @public
     */
    src: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].object])
  },
  computed: {
    placeholder: function placeholder(_ref) {
      var src = _ref.src;
      return !src ? _placeholder : null;
    }
  },
  styles: {
    css: _ImageModule["default"],
    publicClassNames: ['image']
  },
  render: function render(_ref2) {
    var css = _ref2.css,
      rest = _objectWithoutProperties(_ref2, _excluded);
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Image["default"], _objectSpread(_objectSpread({
      draggable: "false"
    }, rest), {}, {
      css: css
    }));
  }
});

// This induces a render when there is a screen size change that has a corresponding image src value
// associated with the new screen size. The render is kicked off by remembering the new image src.
//
// This hoc could (should) be rewritten at a later time to use a smarter context API and callbacks,
// or something like pub/sub; each of which would be hooked together from the resolution.js that
// would coordinate any screen size/orientation changes and emit events from there.
//
// This is ripe for refactoring, and could probably move into UI to be generalized, but that's for
// another time. -B 2018-05-01
var ResponsiveImageDecorator = (0, _hoc["default"])(function (config, Wrapped) {
  var _Class;
  return _Class = /*#__PURE__*/function (_Component) {
    function _Class(_props) {
      var _this;
      _classCallCheck(this, _Class);
      _this = _callSuper(this, _Class, [_props]);
      _this.handleResize = function () {
        _this.setState(function (state, props) {
          var src = (0, _resolution.selectSrc)(props.src);
          // Trigger a render and save the currently selected src for later comparisons
          if (src !== state.src) {
            return {
              src: src
            };
          }
          return null;
        });
      };
      _this.state = {
        src: (0, _resolution.selectSrc)(_this.props.src)
      };
      return _this;
    }
    _inherits(_Class, _Component);
    return _createClass(_Class, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        window.addEventListener('resize', this.handleResize);
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        window.removeEventListener('resize', this.handleResize);
      }
    }, {
      key: "render",
      value: function render() {
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread({}, this.props));
      }
    }]);
  }(_react.Component), _Class.displayName = 'ResponsiveImageDecorator', _Class.propTypes = {
    src: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].object])
  }, _Class;
});

/**
 * agate-specific behaviors to apply to {@link agate/Image.ImageBase|Image}.
 *
 * @hoc
 * @memberof agate/Image
 * @mixes agate/Skinnable.Skinnable
 * @public
 */
var ImageDecorator = exports.ImageDecorator = (0, _compose["default"])(_Pure["default"], ResponsiveImageDecorator, _Skinnable["default"]);

/**
 * An Agate-styled image component
 *
 * Usage:
 * ```
 * <Image
 *   src={{
 *     'hd': 'https://dummyimage.com/64/e048e0/0011ff',
 *     'fhd': 'https://dummyimage.com/128/e048e0/0011ff',
 *     'uhd': 'https://dummyimage.com/256/e048e0/0011ff'
 *   }}
 * >
 * ```
 *
 * @class Image
 * @memberof agate/Image
 * @extends agate/Image.ImageBase
 * @mixes agate/Image.ImageDecorator
 * @ui
 * @public
 */
var Image = exports.Image = ImageDecorator(ImageBase);
var _default = exports["default"] = Image;