import _typeof from '@babel/runtime/helpers/typeof';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.regexp.to-string.js';
import React__default, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Tooltip } from '@carbon/react';
import warning from 'warning';
import { settings } from '../../../constants/Settings.js';
import { WrapCellTextPropTypes } from '../../../constants/SharedPropTypes.js';
import { DefinitionTooltip } from '../../Tooltip/DefinitionTooltip.js';

var iotPrefix = settings.iotPrefix;
var propTypes = {
  children: PropTypes.oneOfType([PropTypes.node, PropTypes.bool, PropTypes.object, PropTypes.array]),
  wrapText: WrapCellTextPropTypes.isRequired,
  truncateCellText: PropTypes.bool.isRequired,
  allowTooltip: PropTypes.bool,
  /** What locale should the number be rendered in */
  locale: PropTypes.string,
  renderDataFunction: PropTypes.func,
  columnId: PropTypes.string,
  rowId: PropTypes.string,
  row: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.node, PropTypes.bool, PropTypes.object, PropTypes.array])),
  /** use white-space: pre; css when true */
  preserveCellWhiteSpace: PropTypes.bool,
  tooltipDirection: PropTypes.string
};
var defaultProps = {
  row: null,
  children: null,
  allowTooltip: true,
  locale: null,
  renderDataFunction: null,
  columnId: null,
  rowId: null,
  preserveCellWhiteSpace: false
};
var isElementTruncated = function isElementTruncated(element) {
  return element.offsetWidth < element.scrollWidth;
};
var renderCustomCell = function renderCustomCell(renderDataFunction, args, className) {
  var result = renderDataFunction(args);
  var title = typeof result === 'string' ? result : undefined;
  return /*#__PURE__*/React__default.createElement("span", {
    className: className,
    title: title
  }, result);
};

/** Supports our default render decisions for primitive values */
var TableCellRenderer = function TableCellRenderer(_ref) {
  var tooltipDirection = _ref.tooltipDirection,
    children = _ref.children,
    wrapText = _ref.wrapText,
    allowTooltip = _ref.allowTooltip,
    truncateCellText = _ref.truncateCellText,
    locale = _ref.locale,
    tooltip = _ref.tooltip,
    renderDataFunction = _ref.renderDataFunction,
    columnId = _ref.columnId,
    rowId = _ref.rowId,
    row = _ref.row,
    isSortable = _ref.isSortable,
    sortFunction = _ref.sortFunction,
    isFilterable = _ref.isFilterable,
    filterFunction = _ref.filterFunction,
    preserveCellWhiteSpace = _ref.preserveCellWhiteSpace;
  var mySpanRef = /*#__PURE__*/React__default.createRef();
  var myClasses = classnames(_defineProperty(_defineProperty(_defineProperty({}, "".concat(iotPrefix, "--table__cell-text--truncate"), wrapText !== 'always' && truncateCellText), "".concat(iotPrefix, "--table__cell-text--no-wrap"), wrapText === 'never' || wrapText === 'expand'), "".concat(iotPrefix, "--table__cell-text--preserve"), preserveCellWhiteSpace));
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    useTooltip = _useState2[0],
    setUseTooltip = _useState2[1];
  var withTooltip = function withTooltip(element, tooltipForExtraInformation) {
    return tooltip ? /*#__PURE__*/React__default.createElement(DefinitionTooltip, {
      triggerClassName: "".concat(iotPrefix, "--table__cell-tooltip"),
      definition: tooltipForExtraInformation,
      id: "table-header-tooltip",
      align: tooltipDirection,
      openOnHover: true,
      tooltipText: tooltip,
      as: "a"
    }, element) : /*#__PURE__*/React__default.createElement(Tooltip, {
      showIcon: false,
      label: element,
      triggerId: "table-cell-tooltip-trigger",
      tooltipId: "table-cell-tooltip",
      autoAlign: true
    }, element);
  };
  useEffect(function () {
    var canBeTruncated = typeof children === 'string' || typeof children === 'number' || typeof children === 'boolean';
    if (canBeTruncated && truncateCellText && allowTooltip && mySpanRef && mySpanRef.current) {
      setUseTooltip(isElementTruncated(mySpanRef.current));
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [mySpanRef.current, children, wrapText, truncateCellText, allowTooltip]);
  {
    var isObject = ! /*#__PURE__*/React__default.isValidElement(children) && _typeof(children) === 'object' && children !== null;
    var missingRender = isObject && typeof renderDataFunction !== 'function';
    var missingSort = isObject && isSortable && typeof sortFunction !== 'function';
    var missingFilter = isObject && isFilterable && typeof filterFunction !== 'function';
    warning(!missingRender, "You must supply a 'renderDataFunction' when passing objects as column values.") ;
    warning(!missingSort, "You must supply a 'sortFunction' when isSortable is true and you're passing objects as column values.") ;
    warning(!missingFilter, "You must supply a 'filterFunction' when passing objects as column values and want them filterable.") ;
  }
  var cellContent = renderDataFunction ? renderCustomCell(renderDataFunction, {
    value: children,
    columnId: columnId,
    rowId: rowId,
    row: row
  }, myClasses) : typeof children === 'string' || typeof children === 'number' ? /*#__PURE__*/React__default.createElement("span", {
    className: myClasses,
    title: useTooltip || tooltip ? undefined : typeof children === 'number' && locale ? children.toLocaleString(locale, {
      maximumFractionDigits: 20
    }) : children,
    ref: mySpanRef
  }, typeof children === 'number' && locale ? children.toLocaleString(locale, {
    maximumFractionDigits: 20
  }) : children) : typeof children === 'boolean' ?
  /*#__PURE__*/
  // handle booleans
  React__default.createElement("span", {
    className: myClasses,
    title: useTooltip || tooltip ? undefined : children.toString()
  }, children.toString()) : _typeof(children) === 'object' && ! /*#__PURE__*/React__default.isValidElement(children) ? null : children;
  return useTooltip || tooltip ? withTooltip(cellContent, tooltip) : cellContent;
};
TableCellRenderer.propTypes = propTypes;
TableCellRenderer.defaultProps = defaultProps;
TableCellRenderer.__docgenInfo = {
  "description": "Supports our default render decisions for primitive values",
  "methods": [],
  "displayName": "TableCellRenderer",
  "props": {
    "row": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "objectOf",
        "value": {
          "name": "union",
          "value": [{
            "name": "node"
          }, {
            "name": "bool"
          }, {
            "name": "object"
          }, {
            "name": "array"
          }]
        }
      },
      "required": false
    },
    "children": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "union",
        "value": [{
          "name": "node"
        }, {
          "name": "bool"
        }, {
          "name": "object"
        }, {
          "name": "array"
        }]
      },
      "required": false
    },
    "allowTooltip": {
      "defaultValue": {
        "value": "true",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "locale": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "What locale should the number be rendered in",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "renderDataFunction": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "columnId": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "rowId": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "preserveCellWhiteSpace": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "use white-space: pre; css when true",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "wrapText": {
      "description": "",
      "type": {
        "name": "custom",
        "raw": "WrapCellTextPropTypes.isRequired"
      },
      "required": false
    },
    "truncateCellText": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "tooltipDirection": {
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    }
  }
};
var TableCellRenderer$1 = TableCellRenderer;

export { TableCellRenderer$1 as default };
