"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.LineGraph = void 0;
const react_1 = __importStar(require("react"));
const react_virtualized_auto_sizer_1 = __importDefault(require("react-virtualized-auto-sizer"));
const utils_1 = require("./utils");
const genLabels_1 = __importDefault(require("./genLabels"));
const genPath_1 = require("./genPath");
const XAxis_1 = __importDefault(require("./XAxis"));
const Labels_1 = __importDefault(require("./Labels"));
const OverlayWrapper_1 = __importDefault(require("./OverlayWrapper"));
const inlines_1 = require("inlines");
const Text_1 = require("../Text");
const LineLegend_1 = require("./LineLegend");
const Graph = ({ width, height, data: dataInput, xFormat, label, valueFormat, legend, style, }) => {
    const labelRef = (0, react_1.useRef)();
    const [labelWidth, updateLabelWidth] = (0, react_1.useState)(0);
    const svgWidth = width - labelWidth;
    const svgHeight = height - (label ? 66 : 32) + (xFormat ? 0 : 16);
    const data = (0, utils_1.processData)({ dataInput });
    const { globalMaxY, globalMinY, globalMaxX, globalMinX } = (0, utils_1.getGlobalMinMax)(data);
    const ySpread = globalMaxY - globalMinY;
    (0, react_1.useEffect)(() => {
        if (labelRef.current) {
            updateLabelWidth(labelRef.current.getBoundingClientRect().width);
        }
    }, [ySpread]);
    const { paths, lineRefs } = (0, genPath_1.genPaths)({
        data: data,
        width: svgWidth,
        height: svgHeight,
    });
    const { labels, labelHeight } = (0, genLabels_1.default)(svgHeight, ySpread, globalMaxY);
    return (react_1.default.createElement(inlines_1.styled.div, { style: {
            width,
            height,
        } },
        label ? (react_1.default.createElement(Text_1.Text, { size: 16, weight: "strong", style: { marginBottom: 24 } }, label)) : null,
        react_1.default.createElement("div", { style: {
                width,
                height: svgHeight,
                display: 'flex',
                paddingTop: 12,
            } },
            react_1.default.createElement("div", { ref: labelRef, style: {
                    marginTop: -32,
                    paddingRight: 24,
                } },
                react_1.default.createElement(Labels_1.default, { labels: labels, labelHeight: labelHeight, valueFormat: valueFormat })),
            react_1.default.createElement(OverlayWrapper_1.default, { isStacked: false, legend: false, width: svgWidth, height: svgHeight, labelHeight: labelHeight, labels: labels, data: data, ySpread: ySpread, lineRefs: lineRefs, xFormat: xFormat }, paths)),
        xFormat ? (react_1.default.createElement("div", { style: {
                paddingLeft: labelWidth + 'px',
            } },
            react_1.default.createElement(XAxis_1.default, { maxX: globalMaxX, minX: globalMinX, xFormat: xFormat, width: svgWidth }))) : null,
        legend && react_1.default.createElement(LineLegend_1.LineLegend, { data: data })));
};
const LineGraph = ({ data, label, xFormat = 'number', valueFormat = 'number-short', legend, }) => {
    return (react_1.default.createElement(react_virtualized_auto_sizer_1.default, null, ({ height, width }) => {
        return (react_1.default.createElement(Graph, { label: label, data: data, height: height, width: width, xFormat: xFormat, valueFormat: valueFormat, legend: legend }));
    }));
};
exports.LineGraph = LineGraph;
//# sourceMappingURL=index.js.map