"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProfileResizablePanes = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var ResizablePanes_1 = require("../ResizablePanes");
var SideButtonsPanel_1 = require("../SideButtonsPanel");
var PerspectivesSettingsContext_1 = require("../contexts/PerspectivesSettingsContext");
var styles_1 = require("./styles");
var DEFAULT_R_PANE_SIZE = 480;
var MIN_R_PANE_SIZE = 320;
var MAX_R_PANE_SIZE = 640;
var ProfileResizablePanes = function (_a) {
    var perspectiveId = _a.perspectiveId, children = _a.children, buttonsProps = _a.buttonsProps, disabled = _a.disabled, defaultTab = _a.defaultTab;
    var styles = (0, styles_1.useStyles)();
    var _b = (0, PerspectivesSettingsContext_1.usePerspectivesSettings)(perspectiveId), perspectiveSettings = _b.perspectiveSettings, updatePerspectiveSettings = _b.updatePerspectiveSettings;
    var _c = (perspectiveSettings || {}).width, width = _c === void 0 ? DEFAULT_R_PANE_SIZE : _c;
    var active = !(0, ramda_1.isNil)(defaultTab) ? defaultTab : perspectiveSettings === null || perspectiveSettings === void 0 ? void 0 : perspectiveSettings.active;
    var activeButton = buttonsProps.find(function (button) { return button.id === active; });
    var isSidePanelOpen = !(0, ramda_1.isNil)(active) && !disabled && Boolean(activeButton);
    var handleUpdateSettings = (0, react_1.useCallback)(function (newSetting) {
        updatePerspectiveSettings(newSetting);
    }, [updatePerspectiveSettings]);
    var toggleActive = (0, react_1.useCallback)(function (newId) {
        var newActive = active === newId ? null : newId;
        handleUpdateSettings({ active: newActive });
    }, [active, handleUpdateSettings]);
    var resetActive = (0, react_1.useCallback)(function () {
        handleUpdateSettings({ active: null });
    }, [handleUpdateSettings]);
    var handlePaneSizeChange = (0, react_1.useCallback)(function (_a) {
        var size = _a.size;
        handleUpdateSettings({ width: size });
    }, [handleUpdateSettings]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(ResizablePanes_1.ResizablePanes, { className: styles.resizablePanesWrapper, primary: "second", orientation: "vertical", size: isSidePanelOpen ? width : 0, minSize: MIN_R_PANE_SIZE, maxSize: MAX_R_PANE_SIZE, allowResize: isSidePanelOpen, onChange: handlePaneSizeChange, debounceInterval: 2500 },
            react_1.default.createElement("div", { className: styles.pane }, children[0]),
            react_1.default.createElement("div", { className: styles.pane }, (0, react_1.cloneElement)(children[1], { active: active, onClose: resetActive }))),
        react_1.default.createElement(SideButtonsPanel_1.SideButtonsPanel, { disabled: disabled, buttonsProps: buttonsProps, onButtonClick: toggleActive, activeIndexId: active })));
};
exports.ProfileResizablePanes = ProfileResizablePanes;
exports.ProfileResizablePanes.displayName = 'ProfileResizablePanes';
