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 throttle from 'lodash.throttle';
import { Sticky } from '../Sticky/index.js';
import { isFunction } from '../../lib/utils.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 { ModalSeparator } from '../Modal/ModalSeparator.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles } from './SidePage.styles.js';
import { SidePageContext } from './SidePageContext.js';
import { SidePageCloseButton } from './SidePageCloseButton.js';
export var SidePageHeaderDataTids = {
    root: 'SidePageHeader__root',
    close: 'SidePage__close',
};
/**
 * Шапка сайдпейджа
 *
 * @visibleName SidePage.Header
 */
var SidePageHeader = /** @class */ (function (_super) {
    __extends(SidePageHeader, _super);
    function SidePageHeader() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.context = _this.context;
        _this.state = {
            isNativeStuck: true,
            isReadyToStuck: false,
        };
        _this.sticky = null;
        _this.componentDidMount = function () {
            var _a, _b, _c, _d, _e;
            (_c = (_b = (_a = getRootNode(_this)) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.addEventListener) === null || _c === void 0 ? void 0 : _c.call(_b, 'scroll', _this.throttleHandleScroll, true);
            (_e = (_d = _this.context).setHasHeader) === null || _e === void 0 ? void 0 : _e.call(_d);
            _this.context.headerRef(_this);
        };
        _this.componentWillUnmount = function () {
            var _a, _b, _c, _d, _e;
            (_c = (_b = (_a = getRootNode(_this)) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.removeEventListener) === null || _c === void 0 ? void 0 : _c.call(_b, 'scroll', _this.throttleHandleScroll, true);
            (_e = (_d = _this.context).setHasHeader) === null || _e === void 0 ? void 0 : _e.call(_d, false);
            _this.context.headerRef(null);
        };
        _this.throttleHandleScroll = throttle(function (event) {
            var target = event.currentTarget;
            if (target && typeof target.scrollTop === 'number') {
                _this.setState({ isNativeStuck: target.scrollTop === 0 });
            }
            _this.update();
        }, 5);
        _this.update = function () {
            var _a;
            (_a = _this.sticky) === null || _a === void 0 ? void 0 : _a.reflow();
        };
        _this.renderHeader = function (fixed) {
            var _a, _b;
            if (fixed === void 0) { fixed = false; }
            var isDisplayed = _this.props.hasSeparator || fixed;
            return (React.createElement("div", null,
                React.createElement("div", { className: _this.cx(_this.styles.header(_this.theme), (_a = {},
                        _a[_this.styles.headerFixed(_this.theme)] = fixed,
                        _a[_this.styles.mobileHeader(_this.theme)] = _this.isMobileLayout,
                        _a)) },
                    _this.renderClose(fixed),
                    React.createElement("div", { className: _this.cx(_this.styles.title(_this.theme), (_b = {},
                            _b[_this.styles.mobileTitle(_this.theme)] = _this.isMobileLayout,
                            _b[_this.styles.titleCut()] = fixed && _this.props.cutTitleOnStuck,
                            _b)) }, isFunction(_this.props.children) ? _this.props.children(fixed) : _this.props.children)),
                isDisplayed && React.createElement(ModalSeparator, { fixed: fixed })));
        };
        _this.renderClose = function (fixed) {
            var _a;
            var stickyOffset = parseInt(_this.theme.sidePageHeaderStickyOffset);
            return (React.createElement("div", { className: _this.cx(_this.styles.wrapperClose(_this.theme), (_a = {},
                    _a[_this.styles.wrapperCloseFixed(_this.theme)] = fixed,
                    _a[_this.styles.mobileWrapperClose(_this.theme)] = _this.isMobileLayout,
                    _a)) }, !_this.isMobileLayout ? (React.createElement(Sticky, { side: "top", offset: stickyOffset },
                React.createElement(SidePageCloseButton, { isHeaderFixed: fixed }))) : (React.createElement(SidePageCloseButton, { isHeaderFixed: fixed, isMobile: _this.isMobileLayout }))));
        };
        _this.stickyRef = function (el) {
            _this.sticky = el;
        };
        return _this;
    }
    SidePageHeader.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();
        }));
    };
    SidePageHeader.prototype.getStickyProp = function () {
        if (typeof this.props.sticky !== 'undefined') {
            return this.props.sticky;
        }
        if (this.isMobileLayout) {
            return false;
        }
        return true;
    };
    SidePageHeader.prototype.renderMain = function () {
        var _a;
        var isStickyDesktop = !this.isMobileLayout && this.getStickyProp();
        var isStickyMobile = this.isMobileLayout && this.getStickyProp();
        var header = this.renderHeader;
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props),
            React.createElement("div", { "data-tid": SidePageHeaderDataTids.root, className: this.cx(this.styles.headerWrapper(), (_a = {},
                    _a[this.styles.headerNativeStuck(this.theme)] = this.state.isNativeStuck,
                    _a)) }, !this.state.isNativeStuck && (isStickyDesktop || isStickyMobile) ? (React.createElement(Sticky, { ref: this.stickyRef, side: "top" }, header)) : (header()))));
    };
    SidePageHeader.__KONTUR_REACT_UI__ = 'SidePageHeader';
    SidePageHeader.displayName = 'SidePageHeader';
    SidePageHeader.contextType = SidePageContext;
    SidePageHeader.defaultProps = {
        hasSeparator: false,
    };
    SidePageHeader = __decorate([
        withRenderEnvironment,
        responsiveLayout,
        rootNode
    ], SidePageHeader);
    return SidePageHeader;
}(React.Component));
export { SidePageHeader };
//# sourceMappingURL=SidePageHeader.js.map