"use strict";
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 __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;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZoomSlider = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var core_1 = require("@react-sigma/core");
var Add_1 = __importDefault(require("@mui/icons-material/Add"));
var Remove_1 = __importDefault(require("@mui/icons-material/Remove"));
var Slider_1 = __importStar(require("@mui/material/Slider"));
var IconButton_1 = __importDefault(require("@mui/material/IconButton"));
var styles_1 = require("./styles");
var percentsToRatio = function (percent) { return Math.pow(100 / percent, 2); };
var ratioToPercents = function (ratio) { return 100 / Math.sqrt(ratio); };
var ZoomSliderThumb = function (props) {
    var children = props.children, other = __rest(props, ["children"]);
    var styles = (0, styles_1.useStyles)();
    return (react_1.default.createElement(Slider_1.SliderThumb, __assign({}, other),
        children,
        react_1.default.createElement(Remove_1.default, { className: (0, classnames_1.default)(styles.icon, styles.thumbIcon) })));
};
var ZoomSlider = function (_a) {
    var className = _a.className, max = _a.max, min = _a.min;
    var styles = (0, styles_1.useStyles)();
    var setSettings = (0, core_1.useSetSettings)();
    var registerEvents = (0, core_1.useRegisterEvents)();
    var camera = (0, core_1.useSigma)().getCamera();
    var _b = (0, core_1.useCamera)({ factor: 1.2 }), zoomIn = _b.zoomIn, zoomOut = _b.zoomOut;
    var _c = (0, react_1.useState)((0, ramda_1.clamp)(min, max, ratioToPercents(camera.getState().ratio))), value = _c[0], setValue = _c[1];
    var maxCameraRatio = (0, react_1.useMemo)(function () { return (min > 0 ? percentsToRatio(min) : null); }, [min]);
    var minCameraRatio = (0, react_1.useMemo)(function () { return (max > 0 ? percentsToRatio(max) : null); }, [max]);
    (0, react_1.useEffect)(function () {
        setSettings({
            maxCameraRatio: maxCameraRatio,
            minCameraRatio: minCameraRatio
        });
        return function () {
            setSettings({
                maxCameraRatio: null,
                minCameraRatio: null
            });
        };
    }, [setSettings, minCameraRatio, maxCameraRatio]);
    (0, react_1.useEffect)(function () {
        registerEvents({
            updated: function (event) { return setValue(ratioToPercents(event.ratio)); }
        });
    }, [registerEvents]);
    var handleChange = function (event, newValue) {
        return camera.setState({ ratio: camera.getBoundedRatio(percentsToRatio(newValue)) });
    };
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)(styles.container, className) },
        react_1.default.createElement(IconButton_1.default, { "data-reltio-id": "zoom-out-button", className: styles.minButton, onClick: function () { return zoomOut(); }, size: "large" },
            react_1.default.createElement(Remove_1.default, { className: styles.icon })),
        react_1.default.createElement(Slider_1.default, { slots: {
                thumb: ZoomSliderThumb
            }, slotProps: {
                thumb: {
                    className: styles.thumb
                }
            }, classes: { root: styles.sliderRoot, rail: styles.rail }, track: false, value: value, min: min, max: max, onChange: handleChange }),
        react_1.default.createElement(IconButton_1.default, { "data-reltio-id": "zoom-in-button", className: styles.maxButton, onClick: function () { return zoomIn(); }, size: "large" },
            react_1.default.createElement(Add_1.default, { className: styles.icon }))));
};
exports.ZoomSlider = ZoomSlider;
