"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createToolbar = void 0;
var react_1 = __importStar(require("react"));
var react_dom_1 = __importDefault(require("react-dom"));
var useFullscreen_1 = __importDefault(require("../../hooks/useFullscreen"));
var utils_1 = require("../../utils");
var Toolbar = function (_a) {
    var _b, _c;
    var toolbarCfg = _a.toolbarCfg, container = _a.container, graph = _a.graph;
    var useGraph = (0, react_1.useRef)();
    var width = (0, react_1.useRef)();
    var height = (0, react_1.useRef)();
    var zoom = (0, react_1.useRef)(1);
    var _d = toolbarCfg.zoomFactor, zoomFactor = _d === void 0 ? 0.25 : _d, renderIcon = toolbarCfg.renderIcon, customContent = toolbarCfg.customContent;
    var _e = (0, useFullscreen_1.default)(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());
    };
    (0, react_1.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('{'))) {
            utils_1.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_1.default.createElement(react_1.Fragment, null,
        !fullscreen ? (react_1.default.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: setToggleFullscreen }, "\u2610")) : (react_1.default.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: setToggleFullscreen }, "\u2684")),
        react_1.default.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: zoomIn }, "+"),
        react_1.default.createElement("span", { style: {
                cursor: 'pointer',
            }, onClick: zoomOut }, "-")));
};
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';
    (0, utils_1.setStyles)(mountPoint, defaultStyle);
    (0, utils_1.setStyles)(mountPoint, style);
    react_dom_1.default.render(react_1.default.createElement(Toolbar, { graph: graph, container: container, toolbarCfg: toolbarCfg }), mountPoint);
    // @ts-ignore
    container.appendChild(mountPoint);
};
exports.createToolbar = createToolbar;
