"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 __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var PropTypes = require("prop-types");
var _ = require("lodash");
var classnames = require("classnames");
var utils_1 = require("../utils");
// 58.25 rem = width of sidebar + width of left column
var COLLAPSE_BREAKPOINT_WIDTH_REM = 63.25;
var propTypes = {
    // external facing
    title: PropTypes.string.isRequired,
    stepNumberInTitle: PropTypes.bool.isRequired,
    description: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
    help: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
    Component: PropTypes.oneOfType([PropTypes.func, PropTypes.instanceOf(React.Component)])
        .isRequired,
    componentProps: PropTypes.object,
    // internal facing
    className: PropTypes.string,
    currentStep: PropTypes.number.isRequired,
    totalSteps: PropTypes.number.isRequired,
    updatePercentComplete: PropTypes.func.isRequired,
    calculatePercentComplete: PropTypes.func.isRequired,
    percentComplete: PropTypes.number.isRequired,
    setWizardState: PropTypes.func.isRequired,
    wizardState: PropTypes.object.isRequired,
};
var WizardStep = /** @class */ (function (_super) {
    __extends(WizardStep, _super);
    function WizardStep(props) {
        var _this = _super.call(this, props) || this;
        _this.component = React.createRef();
        // NOTE: if a webpage explicitly changes the font size on the html element, this may be
        // incorrect, as rem is calculated against the browser-set font-size, not that of the html
        // element.
        // NOTE: this assumes the font size is expressed in px.
        var fontSize = window.getComputedStyle(document.getElementsByTagName("html")[0])["font-size"];
        _this.state = {
            renderedStepWidth: null,
            collapseBreakpointWidth: COLLAPSE_BREAKPOINT_WIDTH_REM * parseFloat(fontSize),
            helpTextCollapsed: false,
        };
        _this.updateDimensions = _this.updateDimensions.bind(_this);
        return _this;
    }
    WizardStep.prototype.componentDidMount = function () {
        this.updateDimensions();
        window.addEventListener("resize", this.updateDimensions);
    };
    WizardStep.prototype.componentWillUnmount = function () {
        window.removeEventListener("resize", this.updateDimensions);
    };
    WizardStep.prototype.updateDimensions = function () {
        var renderedStepWidth = this.component.current.offsetWidth;
        this.setState({
            helpTextCollapsed: this.state.collapseBreakpointWidth > renderedStepWidth,
        });
    };
    WizardStep.prototype.render = function () {
        var _a = this.props, title = _a.title, description = _a.description, Component = _a.Component, setWizardState = _a.setWizardState, currentStep = _a.currentStep, wizardState = _a.wizardState, help = _a.help, percentComplete = _a.percentComplete, calculatePercentComplete = _a.calculatePercentComplete, updatePercentComplete = _a.updatePercentComplete, totalSteps = _a.totalSteps, componentProps = _a.componentProps, className = _a.className, stepNumberInTitle = _a.stepNumberInTitle;
        var props = _.omit(componentProps || {}, ["wizardState", "setWizardState"]);
        var baseClasses = ["Wizard", className]
            .filter(function (c) { return !!c; })
            .map(function (c) { return utils_1.classNameFor(c, "WizardStep"); });
        var contentGroupClass = utils_1.classNameFor(["Wizard", className], "contentGroup");
        return (React.createElement("div", { className: classnames(baseClasses), ref: this.component },
            React.createElement("div", { className: utils_1.classNameFor(baseClasses, "title") },
                React.createElement("h1", null,
                    stepNumberInTitle && "Step " + (currentStep + 1) + ": ",
                    title)),
            React.createElement("div", { className: utils_1.classNameFor(baseClasses, "topInfo") },
                description && (React.createElement("div", { className: classnames(contentGroupClass, utils_1.classNameFor(baseClasses, "description")) }, _.isString(description) ? React.createElement("p", null, description) : description)),
                help && (React.createElement("div", { className: classnames(contentGroupClass, utils_1.classNameFor(baseClasses, "help"), this.state.helpTextCollapsed && utils_1.classNameFor(baseClasses, "helpCollapsed")) }, _.isString(help) ? React.createElement("p", null, help) : help))),
            React.createElement("div", { className: classnames(contentGroupClass, utils_1.classNameFor(baseClasses, "component")) },
                React.createElement(Component, __assign({}, props, { setWizardState: function (modifications) {
                        var newState = setWizardState(modifications);
                        // this conditional updates the progress bar in 2 scenarios:
                        // a) oridnarily, steps update the progress bar once they are navigated away from so
                        // that progress only increases when the user actually moves to the next step (see
                        // Wizard.jumpToPage()). However, the final page must react to validity immediately to
                        // signal completion, so this causes the final page to update the percent complete
                        // upon input rather than solely upon navigation.
                        // b) pages immediately update the progress bar if they become invalid, so that the
                        // incompleteness of the form is reflected in the UI immediately.
                        if (currentStep === totalSteps - 1 ||
                            calculatePercentComplete(newState) < percentComplete) {
                            updatePercentComplete(newState);
                        }
                    }, wizardState: wizardState })))));
    };
    WizardStep.propTypes = propTypes;
    return WizardStep;
}(React.Component));
exports.default = WizardStep;
