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 __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;
};
import React, { useEffect, useMemo, useState } from 'react';
import classnames from 'classnames';
import { clamp } from 'ramda';
import { useCamera, useRegisterEvents, useSigma, useSetSettings } from '@react-sigma/core';
import AddIcon from '@mui/icons-material/Add';
import RemoveIcon from '@mui/icons-material/Remove';
import Slider, { SliderThumb } from '@mui/material/Slider';
import IconButton from '@mui/material/IconButton';
import { useStyles } from './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 = useStyles();
    return (React.createElement(SliderThumb, __assign({}, other),
        children,
        React.createElement(RemoveIcon, { className: classnames(styles.icon, styles.thumbIcon) })));
};
export var ZoomSlider = function (_a) {
    var className = _a.className, max = _a.max, min = _a.min;
    var styles = useStyles();
    var setSettings = useSetSettings();
    var registerEvents = useRegisterEvents();
    var camera = useSigma().getCamera();
    var _b = useCamera({ factor: 1.2 }), zoomIn = _b.zoomIn, zoomOut = _b.zoomOut;
    var _c = useState(clamp(min, max, ratioToPercents(camera.getState().ratio))), value = _c[0], setValue = _c[1];
    var maxCameraRatio = useMemo(function () { return (min > 0 ? percentsToRatio(min) : null); }, [min]);
    var minCameraRatio = useMemo(function () { return (max > 0 ? percentsToRatio(max) : null); }, [max]);
    useEffect(function () {
        setSettings({
            maxCameraRatio: maxCameraRatio,
            minCameraRatio: minCameraRatio
        });
        return function () {
            setSettings({
                maxCameraRatio: null,
                minCameraRatio: null
            });
        };
    }, [setSettings, minCameraRatio, maxCameraRatio]);
    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.createElement("div", { className: classnames(styles.container, className) },
        React.createElement(IconButton, { "data-reltio-id": "zoom-out-button", className: styles.minButton, onClick: function () { return zoomOut(); }, size: "large" },
            React.createElement(RemoveIcon, { className: styles.icon })),
        React.createElement(Slider, { 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.createElement(IconButton, { "data-reltio-id": "zoom-in-button", className: styles.maxButton, onClick: function () { return zoomIn(); }, size: "large" },
            React.createElement(AddIcon, { className: styles.icon }))));
};
