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 __());
    };
})();
import * as classnames from "classnames";
import * as PropTypes from "prop-types";
import * as React from "react";
import * as CopyToClipboard from "react-copy-to-clipboard";
import { Button } from "../Button/Button";
import FlexBox from "../flex/FlexBox";
import ItemAlign from "../flex/ItemAlign";
import "./CopyContainer.less";
var propTypes = {
    children: PropTypes.node,
    copyText: PropTypes.string,
    buttonLabel: PropTypes.string,
    className: PropTypes.string,
    onCopyClick: PropTypes.func,
};
export var cssClass = {
    CONTAINER: "CopyContainer",
    BUTTON: "CopyContainer--button",
};
/**
 * This component is useful when you want to make some text copyable.
 * It wraps the CopyToClipboard component.
 */
var CopyContainer = /** @class */ (function (_super) {
    __extends(CopyContainer, _super);
    function CopyContainer(props) {
        var _this = _super.call(this, props) || this;
        _this._onCopy = _this._onCopy.bind(_this);
        _this.state = {
            copied: false,
        };
        return _this;
    }
    CopyContainer.prototype._onCopy = function () {
        var _this = this;
        var onCopyClick = this.props.onCopyClick;
        this.setState({ copied: true });
        if (onCopyClick)
            onCopyClick();
        window.setTimeout(function () { return _this.setState({ copied: false }); }, 2 * 1000);
    };
    CopyContainer.prototype.render = function () {
        var _a = this.props, children = _a.children, copyText = _a.copyText, buttonLabel = _a.buttonLabel, className = _a.className, _b = _a.buttonType, buttonType = _b === void 0 ? "linkPlain" : _b;
        var copied = this.state.copied;
        var copyLabel = !copied ? buttonLabel || "Copy" : "Copied!";
        return (React.createElement(FlexBox, { alignItems: ItemAlign.CENTER, className: classnames(cssClass.CONTAINER, className) },
            children,
            React.createElement(FlexBox, { alignItems: ItemAlign.START, grow: true },
                React.createElement(CopyToClipboard, { text: copyText || children || "", onCopy: this._onCopy },
                    React.createElement(Button, { className: cssClass.BUTTON, type: buttonType, value: copyLabel })))));
    };
    CopyContainer.propTypes = propTypes;
    return CopyContainer;
}(React.PureComponent));
export default CopyContainer;
