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 { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { getRootNode } from '../../lib/rootNode/getRootNode.js';
import { rootNode } from '../../lib/rootNode/index.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { isInstanceOf } from '../../lib/isInstanceOf.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { Indicator } from './Indicator.js';
import { getStyles } from './Tabs.styles.js';
import { TabsContext } from './TabsContext.js';
import { Tab } from './Tab.js';
export var TabsDataTids = {
    root: 'Tabs__root',
    indicatorRoot: 'Indicator__root',
};
/**
 * Контейнер для компонента [Tab](https://tech.skbkontur.ru/kontur-ui/?path=/docs/react-ui_display-data-tabs-tab--doc). Группирует табы и позволяет управлять их состоянием.
 */
var Tabs = /** @class */ (function (_super) {
    __extends(Tabs, _super);
    function Tabs() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getProps = createPropsGetter(Tabs_1.defaultProps);
        _this.tabs = [];
        _this.tabUpdates = {
            on: function (cb) {
                var index = _this.listeners.push(cb);
                return function () {
                    _this.listeners.splice(index, 1);
                };
            },
        };
        _this.listeners = [];
        _this.shiftFocus = function (fromTab, delta) {
            var tabs = _this.tabs;
            var index = tabs.findIndex(function (x) { return x.id === fromTab; });
            var newIndex = Math.max(0, Math.min(index + delta, tabs.length - 1));
            var tab = tabs[newIndex];
            var tabNode = tab.getNode();
            var htmlNode = getRootNode(tabNode);
            if (isInstanceOf(htmlNode, _this.globalObject.HTMLElement) && typeof htmlNode.focus === 'function') {
                htmlNode.focus();
            }
        };
        _this.notifyUpdate = function () {
            _this.listeners.forEach(function (cb) { return cb(); });
        };
        _this.switchTab = function (id) {
            var _a = _this.props, onValueChange = _a.onValueChange, value = _a.value;
            if (id !== value && onValueChange) {
                onValueChange(id);
            }
        };
        _this.getTab = function (id) {
            var _a = (_this.tabs.find(function (x) { return x.id === id; }) || {}).getNode, getNode = _a === void 0 ? null : _a;
            return getNode && getNode();
        };
        _this.addTab = function (id, getNode) {
            _this.tabs = _this.tabs.concat({ id: id, getNode: getNode });
        };
        _this.removeTab = function (id) {
            _this.tabs = _this.tabs.filter(function (tab) { return tab.id !== id; });
        };
        return _this;
    }
    Tabs_1 = Tabs;
    Tabs.prototype.render = function () {
        var _this = this;
        var _a = this.props, value = _a.value, width = _a.width, children = _a.children, indicatorClassName = _a.indicatorClassName, ariaDescribedby = _a["aria-describedby"];
        var _b = this.getProps(), vertical = _b.vertical, size = _b.size;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            var _a;
            _this.theme = theme;
            return (React.createElement(CommonWrapper, __assign({ rootNodeRef: _this.setRootNode }, _this.props),
                React.createElement("div", { "data-tid": TabsDataTids.root, className: _this.cx((_a = {},
                        _a[_this.styles.rootSmall(_this.theme)] = size === 'small',
                        _a[_this.styles.rootMedium(_this.theme)] = size === 'medium',
                        _a[_this.styles.rootLarge(_this.theme)] = size === 'large',
                        _a[_this.styles.vertical()] = vertical,
                        _a)), style: { width: width }, "aria-describedby": ariaDescribedby },
                    React.createElement(TabsContext.Provider, { value: {
                            vertical: vertical,
                            activeTab: value,
                            size: size,
                            getTab: _this.getTab,
                            addTab: _this.addTab,
                            removeTab: _this.removeTab,
                            notifyUpdate: _this.notifyUpdate,
                            shiftFocus: _this.shiftFocus,
                            switchTab: _this.switchTab,
                        } },
                        children,
                        React.createElement(Indicator, { className: indicatorClassName, tabUpdates: _this.tabUpdates, vertical: _this.getProps().vertical })))));
        }));
    };
    var Tabs_1;
    Tabs.__KONTUR_REACT_UI__ = 'Tabs';
    Tabs.displayName = 'Tabs';
    Tabs.defaultProps = {
        vertical: false,
        size: 'large',
    };
    Tabs.Tab = Tab;
    Tabs = Tabs_1 = __decorate([
        withRenderEnvironment,
        rootNode
    ], Tabs);
    return Tabs;
}(React.Component));
export { Tabs };
//# sourceMappingURL=Tabs.js.map