"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.useTooltipState = void 0;
const React = __importStar(require("react"));
const YagrCore_1 = __importDefault(require("./YagrCore"));
// eslint-disable-next-line prefer-arrow-callback
exports.default = React.forwardRef(function YagrReact({ id, config, className = '', debug, onChartLoad, onSelect, update = 'dynamic' }, ref) {
    const chartRef = React.useRef(null);
    const chart = React.useRef();
    const getChartConfig = React.useCallback(() => {
        var _a, _b;
        return {
            ...config,
            hooks: {
                ...config.hooks,
                load: [
                    ...(((_a = config.hooks) === null || _a === void 0 ? void 0 : _a.load) || []),
                    ...(onChartLoad
                        ? [
                            ({ chart, meta }) => onChartLoad(chart, meta),
                        ]
                        : []),
                ],
                onSelect: [
                    ...(((_b = config.hooks) === null || _b === void 0 ? void 0 : _b.onSelect) || []),
                    ...(onSelect
                        ? [({ from, to }) => onSelect(from, to)]
                        : []),
                ],
            },
        };
    }, [config, onChartLoad, onSelect]);
    React.useImperativeHandle(ref, () => ({
        yagr: () => chart.current,
        domElement: () => chartRef.current,
    }));
    const initChart = React.useCallback(() => {
        if (chartRef.current) {
            chart.current = new YagrCore_1.default(chartRef.current, getChartConfig());
        }
    }, [getChartConfig]);
    React.useEffect(() => {
        if (!chart.current || !config) {
            return;
        }
        chart.current.setConfig(getChartConfig(), update === 'hard');
    }, [getChartConfig, update]);
    React.useEffect(() => {
        initChart();
        return () => {
            var _a;
            (_a = chart.current) === null || _a === void 0 ? void 0 : _a.dispose();
            chart.current = undefined;
        };
    }, []);
    const onClick = React.useCallback((event) => {
        if (chart.current && (event.ctrlKey || event.metaKey) && event.shiftKey) {
            const dataUrl = chart.current
                .toDataUrl()
                .replace('image/png', 'image/octet-stream');
            const a = document.createElement('a');
            a.href = dataUrl;
            a.download = ((debug === null || debug === void 0 ? void 0 : debug.filename) || chart.current.id) + '.png';
            a.click();
        }
    }, [id, chart]);
    return React.createElement("div", { id: id, onClick: onClick, className: `yagr ${className}`, ref: chartRef });
});
const useTooltipState = (yagrRef, tooltipRef) => {
    React.useEffect(() => {
        var _a;
        if (!yagrRef.current || !tooltipRef.current) {
            return;
        }
        const tooltip = tooltipRef.current;
        const yagr = yagrRef.current.yagr();
        if (!yagr || !((_a = yagr === null || yagr === void 0 ? void 0 : yagr.plugins) === null || _a === void 0 ? void 0 : _a.tooltip)) {
            return;
        }
        yagr.plugins.tooltip.on('render', (_, data) => {
            tooltip.onChange(data);
        });
        yagr.plugins.tooltip.on('show', (_, data) => {
            tooltip.onChange(data);
        });
        yagr.plugins.tooltip.on('show', (_, data) => {
            tooltip.onChange(data);
        });
    }, [yagrRef.current]);
};
exports.useTooltipState = useTooltipState;
