'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.regexp.exec.js');
var react = require('@carbon/react');
var classnames = require('classnames');
var PropTypes = require('prop-types');
var React = require('react');
var Settings = require('../../constants/Settings.js');
var Button = require('../Button/Button.js');
var iconsReact = require('@carbon/icons-react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var React__default = /*#__PURE__*/_interopDefault(React);

var iotPrefix = Settings.settings.iotPrefix;
var propTypes = {
  alt: PropTypes__default.default.string,
  id: PropTypes__default.default.string.isRequired,
  isSelected: PropTypes__default.default.bool,
  isWide: PropTypes__default.default.bool,
  toggleImageSelection: PropTypes__default.default.func.isRequired,
  src: PropTypes__default.default.string.isRequired,
  title: PropTypes__default.default.string,
  i18n: PropTypes__default.default.shape({
    deleteButtonLabel: PropTypes__default.default.string
  }),
  /** function called with the image id if they click the delete button */
  onDelete: PropTypes__default.default.func,
  testId: PropTypes__default.default.string
};
var defaultProps = {
  alt: undefined,
  isSelected: false,
  isWide: false,
  title: undefined,
  i18n: {
    deleteButtonLabel: 'Delete'
  },
  onDelete: null,
  testId: 'image-tile'
};
var matchFilename = /([^\\/]+)\.(\w+)$/;
var extractFileName = function extractFileName(path) {
  var fileNameGroupIndex = 1;
  var result = matchFilename.exec(path);
  return result && result[fileNameGroupIndex];
};
var ImageTile = function ImageTile(_ref) {
  var _ref2;
  var alt = _ref.alt,
    id = _ref.id,
    isSelected = _ref.isSelected,
    isWide = _ref.isWide,
    toggleImageSelection = _ref.toggleImageSelection,
    src = _ref.src,
    onDelete = _ref.onDelete,
    deleteButtonLabel = _ref.i18n.deleteButtonLabel,
    title = _ref.title,
    testId = _ref.testId;
  return /*#__PURE__*/React__default.default.createElement(react.SelectableTile, {
    className: classnames__default.default("".concat(iotPrefix, "--image-tile"), _defineProperty__default.default({}, "".concat(iotPrefix, "--image-tile--wide"), isWide)),
    id: id,
    selected: isSelected,
    onChange: toggleImageSelection,
    "data-testid": testId
  }, /*#__PURE__*/React__default.default.createElement("div", {
    "data-testid": "".concat(testId, "-title"),
    className: "".concat(iotPrefix, "--image-tile__title")
  }, /*#__PURE__*/React__default.default.createElement("span", null, (_ref2 = title !== null && title !== void 0 ? title : extractFileName(src)) !== null && _ref2 !== void 0 ? _ref2 : src)), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--image-tile__image-container")
  }, /*#__PURE__*/React__default.default.createElement("img", {
    src: src,
    alt: alt,
    "data-testid": "".concat(testId, "-image")
  }), onDelete ? /*#__PURE__*/React__default.default.createElement(Button, {
    className: "".concat(iotPrefix, "--image-tile__title__delete"),
    renderIcon: iconsReact.TrashCan,
    hasIconOnly: true,
    iconDescription: deleteButtonLabel,
    kind: "ghost",
    onClick: function onClick(event) {
      event.stopPropagation();
      onDelete(id);
    },
    testId: "".concat(testId, "-delete-button")
  }) : null));
};
ImageTile.propTypes = propTypes;
ImageTile.defaultProps = defaultProps;
ImageTile.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ImageTile",
  "props": {
    "alt": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "isSelected": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "isWide": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "title": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  deleteButtonLabel: 'Delete',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "deleteButtonLabel": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "onDelete": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "function called with the image id if they click the delete button",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'image-tile'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "id": {
      "description": "",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "toggleImageSelection": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "src": {
      "description": "",
      "type": {
        "name": "string"
      },
      "required": true
    }
  }
};
var ImageTile$1 = ImageTile;

module.exports = ImageTile$1;
