"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlideDown = void 0;
var react_1 = __importStar(require("react"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
/**
 * Компонент "расхлопа".
 * Позволяет скрывать и отображать контент.
 */
var SlideDown = /** @class */ (function (_super) {
    __extends(SlideDown, _super);
    function SlideDown() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('slide-down');
        _this.state = {
            height: _this.props.isExpanded ? 'auto' : 0,
            isHeightAuto: _this.props.isExpanded,
        };
        _this.slideDown = react_1.createRef();
        _this.slideDownContent = react_1.createRef();
        _this.handleTransitionEnd = function (event) {
            if (event.propertyName === 'height' && _this.props.isExpanded) {
                _this.setAutoHeight();
            }
            if (_this.props.onAnimationEnd) {
                _this.props.onAnimationEnd();
            }
        };
        return _this;
    }
    SlideDown.prototype.componentDidUpdate = function (prevProps) {
        var _a = this.props, isExpanded = _a.isExpanded, onAnimationStart = _a.onAnimationStart;
        if (prevProps.isExpanded !== isExpanded) {
            if (isExpanded) {
                this.setHeightToContentHeight();
            }
            else {
                this.setHeightToNull();
            }
            if (onAnimationStart) {
                onAnimationStart();
            }
        }
    };
    SlideDown.prototype.render = function () {
        return (react_1.default.createElement("div", { className: this.cn(), id: this.props.id, style: { height: this.getHeight() }, onTransitionEnd: this.handleTransitionEnd, ref: this.slideDown, "data-test-id": this.props['data-test-id'] },
            react_1.default.createElement("div", { className: this.cn('content', { expanded: this.state.isHeightAuto }), ref: this.slideDownContent }, this.props.children)));
    };
    SlideDown.prototype.getHeight = function () {
        return this.state.isHeightAuto
            ? 'auto'
            : this.state.height;
    };
    SlideDown.prototype.setHeightToContentHeight = function () {
        if (this.slideDownContent.current) {
            this.setState({
                isHeightAuto: false,
                height: this.slideDownContent.current.offsetHeight,
            });
        }
    };
    SlideDown.prototype.setHeightToNull = function () {
        var _this = this;
        this.setHeightToContentHeight();
        // Заставляем React перерисовать элемент
        this.forceUpdate(function () {
            // Заставляем браузер сделать reflow
            if (_this.slideDown.current) {
                _this.slideDown.current.getBoundingClientRect();
            }
            _this.setState({ height: 0 });
        });
    };
    SlideDown.prototype.setAutoHeight = function () {
        this.setState({
            isHeightAuto: true,
        });
    };
    return SlideDown;
}(react_1.default.PureComponent));
exports.SlideDown = SlideDown;
exports.default = cn_1.withTheme(SlideDown);

//# sourceMappingURL=slide-down.js.map
