import React, { useEffect, useRef, Fragment } from 'react';
import ReactDOM from 'react-dom';
import useFullscreen from '../../hooks/useFullscreen';
import { setStyles, Log } from '../../utils';
var Toolbar = function (_a) {
    var _b, _c;
    var toolbarCfg = _a.toolbarCfg, container = _a.container, graph = _a.graph;
    var useGraph = useRef();
    var width = useRef();
    var height = useRef();
    var zoom = useRef(1);
    var _d = toolbarCfg.zoomFactor, zoomFactor = _d === void 0 ? 0.25 : _d, renderIcon = toolbarCfg.renderIcon, customContent = toolbarCfg.customContent;
    var _e = useFullscreen(container), fullscreen = _e[0], toggleFullscreen = _e[1];
    // 获取当全屏时的窗口大小
    var getWindow = function () {
        return [window.outerWidth, window.outerHeight];
    };
    // 切换全屏时保存 graph 尺寸
    var toggleWidth = function (f) {
        var _a;
        var size = f ? getWindow() : [width.current, height.current];
        (_a = useGraph.current) === null || _a === void 0 ? void 0 : _a.changeSize(size[0], size[1]);
    };
    // 获取缩放中心
    var getCenter = function () {
        if (!container) {
            return {
                x: 0,
                y: 0,
            };
        }
        return {
            x: container.clientWidth / 2,
            y: container.clientHeight / 2,
        };
    };
    // in 放大
    var zoomIn = function () {
        var _a;
        (_a = useGraph.current) === null || _a === void 0 ? void 0 : _a.zoom(Math.min(zoom.current + zoomFactor, 5), getCenter());
    };
    // out 缩小
    var zoomOut = function () {
        var _a;
        (_a = useGraph.current) === null || _a === void 0 ? void 0 : _a.zoom(Math.max(zoom.current - zoomFactor, 0.25), getCenter());
    };
    useEffect(function () {
        if (graph) {
            useGraph.current = graph;
            width.current = container === null || container === void 0 ? void 0 : container.clientWidth;
            height.current = container === null || container === void 0 ? void 0 : container.clientHeight;
        }
    }, [graph]);
    var setToggleFullscreen = function () {
        toggleFullscreen();
        toggleWidth(!document.fullscreenElement);
    };
    var customRender = customContent || renderIcon;
    if (customRender) {
        // 用法升级，提示旧用户
        if (!((_c = (_b = customRender
            .toString()
            .match(/\(([^)]*)\)/)) === null || _b === void 0 ? void 0 : _b[1]) === null || _c === void 0 ? void 0 : _c.includes('{'))) {
            Log.warn("customContent \u7528\u6CD5\u5DF2\u7ECF\u5347\u7EA7\uFF0CcustomContent(zoomIn, xx) => customContent({zoomIn, xx})");
        }
        return customRender({
            zoomIn: zoomIn,
            zoomOut: zoomOut,
            toggleFullscreen: setToggleFullscreen,
            fullscreen: fullscreen,
        });
    }
    return (React.createElement(Fragment, null,
        !fullscreen ? (React.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: setToggleFullscreen }, "\u2610")) : (React.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: setToggleFullscreen }, "\u2684")),
        React.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: zoomIn }, "+"),
        React.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: zoomOut }, "-")));
};
export var createToolbar = function (_a) {
    var _b;
    var graph = _a.graph, container = _a.container, toolbarCfg = _a.toolbarCfg;
    var style = toolbarCfg.style, show = toolbarCfg.show, className = toolbarCfg.className;
    var toolbarId = "".concat(graph.get('id'), "-toolbar");
    var exist = document.querySelector("#".concat(toolbarId));
    if (exist) {
        (_b = exist.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(exist);
    }
    if (!show) {
        return;
    }
    var defaultStyle = {
        position: 'absolute',
        right: '12px',
        top: '12px',
        display: 'flex',
        flexDirection: 'column',
        padding: '6px',
        borderRadius: '2px',
        fontSize: '24px',
        textAlign: 'center',
        lineHeight: '24px',
        color: 'rgba(0,0,0,.65)',
        backgroundColor: '#fff',
        boxShadow: '0 0 3px #ccc',
    };
    var mountPoint = document.createElement('div');
    mountPoint.id = toolbarId;
    mountPoint.className = className !== null && className !== void 0 ? className : 'charts-toolbar';
    setStyles(mountPoint, defaultStyle);
    setStyles(mountPoint, style);
    ReactDOM.render(React.createElement(Toolbar, { graph: graph, container: container, toolbarCfg: toolbarCfg }), mountPoint);
    // @ts-ignore
    container.appendChild(mountPoint);
};
