import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.regexp.exec.js';
import { SelectableTile } from '@carbon/react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import React__default from 'react';
import { settings } from '../../constants/Settings.js';
import Button from '../Button/Button.js';
import { TrashCan } from '@carbon/icons-react';

var iotPrefix = settings.iotPrefix;
var propTypes = {
  alt: PropTypes.string,
  id: PropTypes.string.isRequired,
  isSelected: PropTypes.bool,
  isWide: PropTypes.bool,
  toggleImageSelection: PropTypes.func.isRequired,
  src: PropTypes.string.isRequired,
  title: PropTypes.string,
  i18n: PropTypes.shape({
    deleteButtonLabel: PropTypes.string
  }),
  /** function called with the image id if they click the delete button */
  onDelete: PropTypes.func,
  testId: PropTypes.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.createElement(SelectableTile, {
    className: classnames("".concat(iotPrefix, "--image-tile"), _defineProperty({}, "".concat(iotPrefix, "--image-tile--wide"), isWide)),
    id: id,
    selected: isSelected,
    onChange: toggleImageSelection,
    "data-testid": testId
  }, /*#__PURE__*/React__default.createElement("div", {
    "data-testid": "".concat(testId, "-title"),
    className: "".concat(iotPrefix, "--image-tile__title")
  }, /*#__PURE__*/React__default.createElement("span", null, (_ref2 = title !== null && title !== void 0 ? title : extractFileName(src)) !== null && _ref2 !== void 0 ? _ref2 : src)), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(iotPrefix, "--image-tile__image-container")
  }, /*#__PURE__*/React__default.createElement("img", {
    src: src,
    alt: alt,
    "data-testid": "".concat(testId, "-image")
  }), onDelete ? /*#__PURE__*/React__default.createElement(Button, {
    className: "".concat(iotPrefix, "--image-tile__title__delete"),
    renderIcon: 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;

export { ImageTile$1 as default };
