"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Styles = _interopRequireDefault(require("../../Styles"));
var _reactNativeFastImage = _interopRequireDefault(require("react-native-fast-image"));
var _components = require("../../components");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
const styles = _reactNative.StyleSheet.create({
  marker: {
    position: 'absolute',
    top: 10,
    right: 10,
    backgroundColor: 'transparent',
    width: 20,
    height: 20,
    borderWidth: 2,
    borderRadius: 10,
    borderColor: _Styles.default.primary
  }
});
class ImageItem extends _react.Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "arrayObjectIndexOf", (array, property, value) => array.map(o => o[property]).indexOf(value));
  }
  componentWillMount() {
    let {
      width
    } = _reactNative.Dimensions.get('window');
    const {
      imageMargin,
      imagesPerRow,
      containerWidth
    } = this.props;
    if (typeof containerWidth !== 'undefined') {
      width = containerWidth;
    }
    this.imageSize = (width - (imagesPerRow + 1) * imageMargin) / imagesPerRow;
  }
  handleClick(item) {
    this.props.onClick(item);
  }
  fancyTimeFormat(duration) {
    try {
      // Hours, minutes and seconds
      const hrs = ~~(duration / 3600);
      const mins = ~~(duration % 3600 / 60);
      const secs = ~~duration % 60;

      // Output like "1:01" or "4:03:59" or "123:03:59"
      let ret = "";
      if (hrs > 0) {
        ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
      }
      ret += "" + mins + ":" + (secs < 10 ? "0" : "");
      ret += "" + secs;
      return ret;
    } catch (e) {
      return '';
    }
  }
  render() {
    var _item$node$type;
    const {
      item,
      selected,
      selectedMarker,
      imageMargin,
      selectedList
    } = this.props;
    const {
      image
    } = item.node;
    const indexSelected = this.arrayObjectIndexOf(selectedList ? selectedList : [], 'uri', image.uri);
    const marker = /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: [styles.marker, {
        width: 20,
        height: 20,
        backgroundColor: selected ? _Styles.default.primary : 'transparent',
        justifyContent: 'center',
        alignItems: 'center'
      }]
    }, selected && /*#__PURE__*/_react.default.createElement(_components.MText, {
      style: {
        position: 'absolute',
        fontWeight: '600',
        fontSize: 15,
        color: 'white',
        textAlign: 'center'
      }
    }, indexSelected + 1));
    return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
      style: {
        marginBottom: imageMargin,
        marginRight: imageMargin
      },
      onPress: () => this.handleClick(image)
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Image, {
      source: {
        uri: image.uri
      },
      style: {
        height: this.imageSize,
        width: this.imageSize,
        backgroundColor: 'grey'
      }
    }), marker, ((_item$node$type = item.node.type) === null || _item$node$type === void 0 ? void 0 : _item$node$type.includes('video')) && /*#__PURE__*/_react.default.createElement(_components.MText, {
      style: {
        position: 'absolute',
        fontSize: 12,
        color: '#fffffffa',
        bottom: 10,
        right: 8
      }
    }, this.fancyTimeFormat(image.playableDuration)));
  }
}
ImageItem.defaultProps = {
  item: {},
  selected: false
};
ImageItem.propTypes = {
  item: _propTypes.default.object,
  selected: _propTypes.default.bool,
  selectedMarker: _propTypes.default.element,
  imageMargin: _propTypes.default.number,
  imagesPerRow: _propTypes.default.number,
  onClick: _propTypes.default.func
};
var _default = ImageItem;
exports.default = _default;
//# sourceMappingURL=ImageItem.js.map