"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genPaths = exports.genPathCurve = void 0;
// @ts-nocheck
const react_1 = __importStar(require("react"));
const varsUtilities_1 = require("../../varsUtilities");
const utils_1 = require("./utils");
const genPathCurve = (points, r) => {
    let d = `M${points[0].x},${points[0].y}`;
    for (let i = 1; i < points.length - 1; i++) {
        const previous = i - 1;
        const next = i + 1;
        const c = {
            x: points[i].x,
            y: points[i].y,
        };
        const a1 = Math.atan2(points[previous].y - c.y, points[previous].x - c.x);
        const a2 = Math.atan2(points[next].y - c.y, points[next].x - c.x);
        const x1 = c.x + r * Math.cos(a1);
        const y1 = c.y + r * Math.sin(a1);
        const x2 = c.x + r * Math.cos(a2);
        const y2 = c.y + r * Math.sin(a2);
        d += 'L' + x1 + ',' + y1 + ' Q' + c.x + ',' + c.y + ' ' + x2 + ',' + y2;
    }
    return (d += `L${points[points.length - 1].x},${points[points.length - 1].y}`);
};
exports.genPathCurve = genPathCurve;
const makeLine = ({ points, fill, width, height, stepSize, baseColor = 'violet', }) => {
    // const points: [x: number, y: number][] = []
    // for (let i = 0; i < data.length; i++) {
    //   points.push([stepSize * i, (ySpread - (data[i].y - minY)) / pxValue])
    // }
    const lineRef = (0, react_1.useRef)();
    const p = (0, exports.genPathCurve)(points, stepSize / 2);
    return {
        path: [
            fill ? (react_1.default.createElement("path", { key: p, d: p + `L${width},${height},L0,${height}`, fill: (0, varsUtilities_1.color)('nonSemanticBackground', baseColor, 'strong'), fillOpacity: 0.08 })) : null,
            react_1.default.createElement("path", { key: p, ref: lineRef, d: p, fill: "none", stroke: (0, varsUtilities_1.color)('nonSemanticBackground', baseColor, 'strong'), "data-custom": "line", strokeWidth: 2 }),
            // false,
            // ? points.map(({ x, y }, i) => (
            //     <circle key={i} cx={x} cy={y} r="2" fill="red" />
            //   ))
            // : null,
        ],
        lineRef,
    };
};
const genPaths = ({ data, width, height, }) => {
    let lineRefs = {};
    let { globalMaxX, globalMinX, globalMaxY, globalMinY } = (0, utils_1.getGlobalMinMax)(data);
    const xSpread = globalMaxX - globalMinX;
    let ySpread = globalMaxY - globalMinY;
    Object.keys(data).forEach((key) => {
        const paddingLeft = (data[key].minX - globalMinX) / xSpread;
        const paddingRight = (globalMaxX - data[key].maxX) / xSpread;
        const lineWidth = width - Math.abs(width * paddingLeft - width * paddingRight);
        let lineStepSize = lineWidth / (data[key].data.length - 1);
        data[key].stepSize = lineStepSize;
        // let stepSize = width / (data[key].data.length - 1)
        let pxValue = ySpread / height;
        const targetStepSize = 10;
        // TODO: this needs to be done before min max
        if (lineStepSize < targetStepSize) {
            const { data: newData, stepSize: newStepSize, minData, maxData, } = (0, utils_1.averageData)({
                data: data[key].data,
                lineStepSize,
                width: lineWidth,
                targetStepSize,
            });
            data[key].data = newData;
            lineStepSize = newStepSize;
            if (data[key].minMax) {
                data[key].minData = minData;
                data[key].minPoints = (0, utils_1.generatePoints)({
                    data: minData,
                    paddingLeft,
                    width,
                    lineStepSize,
                    ySpread,
                    globalMinY,
                    pxValue,
                });
                data[key].maxData = maxData;
                data[key].maxPoints = (0, utils_1.generatePoints)({
                    data: maxData,
                    paddingLeft,
                    width,
                    lineStepSize,
                    ySpread,
                    globalMinY,
                    pxValue,
                });
            }
            else {
                const { minY, maxY } = (0, utils_1.getMinMax)(data[key].data);
                data[key].minY = minY;
                data[key].maxY = maxY;
                ({ globalMaxY, globalMinY } = (0, utils_1.getGlobalMinMax)(data));
                ySpread = globalMaxY - globalMinY;
                pxValue = ySpread / height;
            }
        }
        data[key].points = (0, utils_1.generatePoints)({
            data: data[key].data,
            paddingLeft,
            width,
            lineStepSize,
            ySpread,
            globalMinY,
            pxValue,
        });
    });
    let paths = [];
    Object.keys(data).map((key) => {
        if (!data[key].points.length) {
            return null;
        }
        const result = makeLine({
            points: data[key].points,
            fill: data[key].fill,
            width,
            height,
            stepSize: data[key].stepSize,
            baseColor: data[key].color
                ? data[key].color
                : data[key].label
                    ? (0, varsUtilities_1.colorHash)('nonSemanticBackground', data[key].label)
                    : 'violet',
        });
        paths = paths.concat(result.path);
        lineRefs = {
            ...lineRefs,
            [key]: result.lineRef,
        };
        return null;
    });
    return {
        paths,
        lineRefs,
    };
};
exports.genPaths = genPaths;
//# sourceMappingURL=genPath.js.map