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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        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);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import * as LayoutEvents from '../../lib/LayoutEvents.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { responsiveLayout } from '../ResponsiveLayout/decorator.js';
import { getRootNode, rootNode } from '../../lib/rootNode/index.js';
import { getDOMRect } from '../../lib/dom/getDOMRect.js';
import { Gapped } from '../Gapped/index.js';
import { isNonNullable } from '../../lib/utils.js';
import { ModalSeparator } from '../Modal/ModalSeparator.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles } from './SidePage.styles.js';
import { SidePageContext } from './SidePageContext.js';
export var SidePageFooterDataTids = {
    root: 'SidePageFooter__root',
};
/**
 * Футер сайдпейджа.
 *
 * @visibleName SidePage.Footer
 */
var SidePageFooter = /** @class */ (function (_super) {
    __extends(SidePageFooter, _super);
    function SidePageFooter() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.context = _this.context;
        _this.state = {
            fixed: false,
        };
        _this.content = null;
        _this.layoutSub = null;
        _this.update = function () {
            _this.setProperStyles();
        };
        _this.refContent = function (node) {
            _this.content = node;
        };
        _this.setProperStyles = function () {
            var wrapper = getRootNode(_this);
            if (wrapper && _this.content) {
                var wrapperRect = getDOMRect(wrapper);
                var contentRect = getDOMRect(_this.content);
                var fixed = wrapperRect.top > contentRect.top;
                _this.setState({ fixed: fixed });
            }
        };
        return _this;
    }
    SidePageFooter.prototype.componentDidMount = function () {
        var _a, _b, _c, _d;
        this.context.footerRef(this);
        this.update();
        this.layoutSub = LayoutEvents.addListener(this.update, this.globalObject);
        (_b = (_a = this.context).setHasFooter) === null || _b === void 0 ? void 0 : _b.call(_a);
        (_d = (_c = this.context).setHasPanel) === null || _d === void 0 ? void 0 : _d.call(_c, this.props.panel);
    };
    SidePageFooter.prototype.componentDidUpdate = function (prevProps) {
        var _a, _b;
        this.props.panel !== prevProps.panel && ((_b = (_a = this.context).setHasPanel) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.panel));
    };
    SidePageFooter.prototype.componentWillUnmount = function () {
        var _a, _b, _c, _d;
        this.context.footerRef(null);
        if (this.layoutSub) {
            this.layoutSub.remove();
        }
        (_b = (_a = this.context).setHasFooter) === null || _b === void 0 ? void 0 : _b.call(_a, false);
        (_d = (_c = this.context).setHasPanel) === null || _d === void 0 ? void 0 : _d.call(_c, false);
    };
    SidePageFooter.prototype.getSticky = function () {
        if (typeof this.props.sticky !== 'undefined') {
            return this.props.sticky;
        }
        if (this.isMobileLayout) {
            return false;
        }
        return true;
    };
    SidePageFooter.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return _this.renderMain();
        }));
    };
    SidePageFooter.prototype.renderMain = function () {
        var _this = this;
        var separator = (this.props.panel || this.state.fixed) && (React.createElement(ModalSeparator, { fixed: this.state.fixed }));
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props),
            React.createElement("div", { "data-tid": SidePageFooterDataTids.root, style: { height: this.getContentHeight() }, className: this.styles.footerWrapper() },
                React.createElement(SidePageContext.Consumer, null, function (_a) {
                    var _b, _c;
                    var getWidth = _a.getWidth;
                    return (React.createElement("div", { className: _this.cx(_this.styles.footer(_this.theme), (_b = {},
                            _b[_this.styles.positionStatic()] = !_this.getSticky(),
                            _b)), style: {
                            width: getWidth(),
                        } },
                        separator,
                        React.createElement("div", { className: _this.cx(_this.styles.footerContent(_this.theme), (_c = {},
                                _c[_this.styles.footerFixed(_this.theme)] = _this.state.fixed,
                                _c[_this.styles.panel(_this.theme)] = !!_this.props.panel,
                                _c[_this.styles.panelFixed(_this.theme)] = !!_this.props.panel && _this.state.fixed,
                                _c[_this.styles.mobileFooterContent(_this.theme)] = _this.isMobileLayout,
                                _c)), ref: _this.refContent }, isNonNullable(_this.props.gap) ? (React.createElement(Gapped, { vertical: _this.isMobileLayout, gap: _this.props.gap }, _this.props.children)) : (_this.props.children))));
                }))));
    };
    SidePageFooter.prototype.getContentHeight = function () {
        if (!this.content) {
            return 'auto';
        }
        return getDOMRect(this.content).height;
    };
    SidePageFooter.__KONTUR_REACT_UI__ = 'SidePageFooter';
    SidePageFooter.displayName = 'SidePageFooter';
    SidePageFooter.contextType = SidePageContext;
    SidePageFooter = __decorate([
        withRenderEnvironment,
        responsiveLayout,
        rootNode
    ], SidePageFooter);
    return SidePageFooter;
}(React.Component));
export { SidePageFooter };
//# sourceMappingURL=SidePageFooter.js.map