"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfirmButton = void 0;
const React = __importStar(require("react"));
const react_dom_1 = require("react-dom");
const ReactContext_1 = __importDefault(require("../../ReactContext"));
const components_1 = require("../components");
class ConfirmButton extends React.Component {
    constructor(props) {
        super(props);
        this.buttonRef = React.createRef();
        this.onButtonKeyDown = ({ key }) => {
            if (key === "Enter")
                this.onConfirmedClick();
            else if (key === "Escape")
                this.setState({ show: false });
        };
        this.onHideConfirm = () => {
            this.setState({ show: false });
        };
        this.onShowConfirm = (e) => {
            e.preventDefault();
            e.stopPropagation();
            this.setState({ show: true }, () => {
                if (this.props.confirmStyle === "browser") {
                    this.browserConfirm();
                }
            });
        };
        this.onCancelClick = (e) => {
            e.preventDefault();
            e.stopPropagation();
            this.onHideConfirm();
        };
        this.onConfirmedClick = (e) => {
            e && e.preventDefault();
            e && e.stopPropagation();
            this.props.onClick();
            this.onHideConfirm();
        };
        this.onClick = (e) => {
            this.props.confirm ? this.onShowConfirm(e) : this.onConfirmedClick(e);
        };
        this.setConfirmAutofocus = (elem) => {
            setTimeout(() => {
                var _a;
                (_a = (0, react_dom_1.findDOMNode)(elem)) === null || _a === void 0 ? void 0 : _a.focus();
            });
        };
        this.renderConfirm = () => {
            const { show } = this.state;
            if (!show) {
                return null;
            }
            const { confirmStyle = "popup" } = this.props;
            if (confirmStyle === "popup") {
                return this.renderConfirmPopup();
            }
            return null;
        };
        this.getOverlayTarget = () => (0, react_dom_1.findDOMNode)(this.buttonRef.current);
        this.state = { show: false };
    }
    render() {
        const { props } = this;
        const { tooltip, disabled, readonly, confirm, confirmPlacement, confirmStyle, prompt, confirmButtonVariant, confirmButtonText, confirmButtonId, cancelButtonId } = props, maybeProps = __rest(props, ["tooltip", "disabled", "readonly", "confirm", "confirmPlacement", "confirmStyle", "prompt", "confirmButtonVariant", "confirmButtonText", "confirmButtonId", "cancelButtonId"]);
        const button = React.createElement(React.Fragment, null,
            React.createElement(components_1.Button, Object.assign({}, maybeProps, { disabled: disabled || readonly, style: this.props.style, ref: this.buttonRef, onKeyDown: this.onButtonKeyDown, onClick: this.onClick }), this.props.children),
            this.renderConfirm());
        return tooltip ? React.createElement(components_1.TooltipComponent, { tooltip: tooltip }, button) : button;
    }
    renderConfirmPopup() {
        const { translations, confirmPlacement = "left", prompt, confirmButtonVariant, confirmButtonText, confirmButtonId, cancelButtonId } = this.props;
        const { Overlay, Popover, ButtonGroup } = this.context.theme;
        return (React.createElement(Overlay, { show: true, placement: confirmPlacement, rootClose: true, onHide: this.onHideConfirm, target: this.getOverlayTarget },
            React.createElement(Popover, { id: `${this.props.id}-button-confirm` },
                React.createElement("div", null, prompt),
                React.createElement(ButtonGroup, null,
                    React.createElement(components_1.Button, { variant: confirmButtonVariant || "primary", onClick: this.onConfirmedClick, ref: this.setConfirmAutofocus, id: confirmButtonId || `${this.props.id}-confirm-yes` }, confirmButtonText || "OK"),
                    React.createElement(components_1.Button, { variant: "default", onClick: this.onCancelClick, id: cancelButtonId || `${this.props.id}-confirm-no` }, translations.Cancel)))));
    }
    browserConfirm() {
        const choice = confirm(this.props.prompt);
        if (choice) {
            this.onConfirmedClick();
        }
        else {
            this.onHideConfirm();
        }
    }
}
exports.ConfirmButton = ConfirmButton;
ConfirmButton.contextType = ReactContext_1.default;
