"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var classnames = require("classnames");
var PropTypes = require("prop-types");
var React = require("react");
var Button_1 = require("../Button/Button");
var RichText_1 = require("../RichText/RichText");
var Tooltip_1 = require("../Tooltip");
var propTypes = {
    className: PropTypes.string,
    text: PropTypes.string.isRequired,
    showMoreLabel: PropTypes.string,
    showLessLabel: PropTypes.string,
    showTooltip: PropTypes.bool,
    lines: PropTypes.number,
    maxCharsShown: PropTypes.number,
    useRichText: PropTypes.bool,
    name: PropTypes.string,
};
var defaultProps = {
    maxCharsShown: 300,
    showMoreLabel: "Show more",
    showLessLabel: "Show less",
    showTooltip: false,
    useRichText: false,
};
var cssClass = {
    CONTAINER: "TextTruncate",
};
/**
 * Truncate long text
 * Comes with read more/less toggle
 */
var TextTruncate = /** @class */ (function (_super) {
    __extends(TextTruncate, _super);
    function TextTruncate() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            truncated: true,
        };
        _this.toggleTruncation = function (event) {
            event.preventDefault();
            _this.setState({ truncated: !_this.state.truncated });
        };
        return _this;
    }
    TextTruncate.prototype.truncate = function (text) {
        return text.substring(0, this.props.maxCharsShown);
    };
    TextTruncate.prototype.render = function () {
        var _a = this.props, className = _a.className, text = _a.text, showMoreLabel = _a.showMoreLabel, showLessLabel = _a.showLessLabel, showTooltip = _a.showTooltip, maxCharsShown = _a.maxCharsShown, useRichText = _a.useRichText, name = _a.name;
        var truncated = this.state.truncated;
        // If text is null or undefined, return immediately
        if (!text) {
            return null;
        }
        var isTextTruncated = text.length > maxCharsShown;
        if (!isTextTruncated) {
            return (React.createElement("div", { className: classnames(cssClass.CONTAINER, className) }, useRichText ? React.createElement(RichText_1.default, { text: text }) : text));
        }
        var displayText = truncated ? this.truncate(text) + "\u2026" : text;
        var toggleText = truncated ? showMoreLabel : showLessLabel;
        var truncatedText = (React.createElement("div", { className: classnames(cssClass.CONTAINER, className) },
            useRichText ? React.createElement(RichText_1.default, { text: displayText }) : displayText,
            " ",
            React.createElement(Button_1.Button, { type: "linkPlain", onClick: this.toggleTruncation, value: toggleText, ariaLabel: name && toggleText + ": " + name })));
        if (showTooltip) {
            return (React.createElement(Tooltip_1.default, { content: text, placement: Tooltip_1.default.Placement.BOTTOM }, truncatedText));
        }
        return truncatedText;
    };
    TextTruncate.propTypes = propTypes;
    TextTruncate.defaultProps = defaultProps;
    return TextTruncate;
}(React.PureComponent));
exports.default = TextTruncate;
