export function getHorizontalFunnelSegmentGeometry(_a) {
    var d = _a.d, i = _a.i, data = _a.data, funnelWidth = _a.funnelWidth, funnelHeight = _a.funnelHeight, isRTL = _a.isRTL;
    var segmentHeight = funnelHeight / data.length;
    var widthScale = function (value) { return (value / Math.max.apply(Math, data.map(function (dataPoint) { return dataPoint.value; }))) * funnelWidth; };
    var topWidth = widthScale(d.value);
    var bottomWidth = i < data.length - 1 ? widthScale(data[i + 1].value) : 0;
    var xOffset = (funnelWidth - topWidth) / 2;
    var nextXOffset = (funnelWidth - bottomWidth) / 2;
    var xStart = isRTL ? funnelWidth - xOffset : xOffset;
    var xEnd = isRTL ? funnelWidth - nextXOffset : nextXOffset;
    var isLastSegment = i === data.length - 1;
    var textY = isLastSegment ? i * segmentHeight + segmentHeight * 0.33 : i * segmentHeight + segmentHeight / 2;
    var textX = funnelWidth / 2;
    var availableWidth = topWidth;
    if (isLastSegment) {
        var yFromTop = textY - i * segmentHeight;
        var widthAtY = topWidth * (1 - yFromTop / segmentHeight);
        availableWidth = Math.max(widthAtY * 0.8, 0);
    }
    else {
        availableWidth = Math.min(topWidth, bottomWidth) * 0.9;
    }
    var pathD = "M".concat(xStart, ",").concat(i * segmentHeight, "\n    L").concat(funnelWidth - xStart, ",").concat(i * segmentHeight, "\n    L").concat(funnelWidth - xEnd, ",").concat((i + 1) * segmentHeight, "\n    L").concat(xEnd, ",").concat((i + 1) * segmentHeight, "\n    Z");
    return { pathD: pathD, textX: textX, textY: textY, availableWidth: availableWidth };
}
export function getVerticalFunnelSegmentGeometry(_a) {
    var d = _a.d, i = _a.i, data = _a.data, funnelWidth = _a.funnelWidth, funnelHeight = _a.funnelHeight, isRTL = _a.isRTL;
    var segmentWidth = funnelWidth / data.length;
    var heightScale = function (value) { return (value / Math.max.apply(Math, data.map(function (dataPoint) { return dataPoint.value; }))) * funnelHeight; };
    var leftHeight = heightScale(d.value);
    var rightHeight = i < data.length - 1 ? heightScale(data[i + 1].value) : 0;
    var yOffset = (funnelHeight - leftHeight) / 2;
    var nextYOffset = (funnelHeight - rightHeight) / 2;
    var x0 = i * segmentWidth;
    var x1 = (i + 1) * segmentWidth;
    var isLastSegment = i === data.length - 1;
    var textX;
    var textY;
    var availableWidth = segmentWidth * 0.8;
    if (isLastSegment) {
        // For the triangular last segment, position text at 1/4 from the left edge
        textX = x0 + (x1 - x0) * 0.25;
        textY = funnelHeight / 2;
        // For triangular segments, we need to check both height and width constraints
        // The segment needs to be large enough to contain text
        var segmentArea = (leftHeight * segmentWidth) / 2; // Area of triangle
        var minAreaForText = 800; // Minimum area needed to show text
        if (leftHeight < 40 || segmentArea < minAreaForText) {
            // Hide text if height is too small or area is insufficient
            availableWidth = 0;
        }
        else {
            // Calculate available width at text position
            var widthAtTextPosition = (x1 - x0) * 0.75;
            availableWidth = widthAtTextPosition * 0.6;
        }
    }
    else {
        textX = (x0 + x1) / 2;
        textY = funnelHeight / 2;
        var minHeight = Math.min(leftHeight, rightHeight);
        availableWidth = minHeight > 20 ? segmentWidth * 0.8 : 0;
    }
    var pathD = "M".concat(x0, ",").concat(yOffset, "\n    L").concat(x1, ",").concat(nextYOffset, "\n    L").concat(x1, ",").concat(funnelHeight - nextYOffset, "\n    L").concat(x0, ",").concat(funnelHeight - yOffset, "\n    Z");
    return { pathD: pathD, textX: textX, textY: textY, availableWidth: availableWidth };
}
export function getStackedHorizontalFunnelSegmentGeometry(_a) {
    var _b, _c;
    var i = _a.i, k = _a.k, stages = _a.stages, totals = _a.totals, maxTotal = _a.maxTotal, funnelWidth = _a.funnelWidth, funnelHeight = _a.funnelHeight;
    var segmentHeight = funnelHeight / stages.length;
    var cur = stages[i];
    var next = stages[i + 1] || { subValues: [] };
    var curTotal = totals[i] || 1;
    var nextTotal = totals[i + 1] || 0;
    var cumTop = 0;
    var cumBot = 0;
    var _loop_1 = function (idx) {
        var v_1 = cur.subValues[idx];
        var vNext_1 = (_b = next.subValues) === null || _b === void 0 ? void 0 : _b.find(function (x) { return x.category === v_1.category; });
        var val_1 = v_1.value;
        var nextVal_1 = vNext_1 ? vNext_1.value : 0;
        cumTop += (val_1 / curTotal) * (curTotal / maxTotal) * funnelWidth;
        cumBot += (nextVal_1 / nextTotal || 0) * (nextTotal / maxTotal) * funnelWidth;
    };
    for (var idx = 0; idx < k; idx++) {
        _loop_1(idx);
    }
    var v = cur.subValues[k];
    var vNext = (_c = next.subValues) === null || _c === void 0 ? void 0 : _c.find(function (x) { return x.category === v.category; });
    var val = v.value;
    var nextVal = vNext ? vNext.value : 0;
    var topW = (val / curTotal) * (curTotal / maxTotal) * funnelWidth;
    var botW = (nextVal / nextTotal || 0) * (nextTotal / maxTotal) * funnelWidth;
    var topStart = (funnelWidth - (curTotal / maxTotal) * funnelWidth) / 2 + cumTop;
    var topEnd = topStart + topW;
    var botStart = (funnelWidth - (nextTotal / maxTotal) * funnelWidth) / 2 + cumBot;
    var botEnd = botStart + botW;
    var textX = (topStart + topEnd + botStart + botEnd) / 4;
    var isLastSegment = i === stages.length - 1;
    var textY = isLastSegment ? i * segmentHeight + segmentHeight * 0.33 : (i + 0.5) * segmentHeight;
    // Calculate available width based on this specific segment's width
    var availableWidth;
    if (isLastSegment) {
        // For triangular last segment, use the width at text Y position
        var yFromTop = textY - i * segmentHeight;
        var widthRatio = 1 - yFromTop / segmentHeight;
        availableWidth = topW * widthRatio;
    }
    else {
        // For trapezoidal segments, use the actual segment width
        availableWidth = Math.min(topW, botW);
    }
    var pathD = "M".concat(topStart, ",").concat(i * segmentHeight, "\n    L").concat(topEnd, ",").concat(i * segmentHeight, "\n    L").concat(botEnd, ",").concat((i + 1) * segmentHeight, "\n    L").concat(botStart, ",").concat((i + 1) * segmentHeight, "\n    Z");
    return { pathD: pathD, textX: textX, textY: textY, availableWidth: availableWidth };
}
export function getStackedVerticalFunnelSegmentGeometry(_a) {
    var _b, _c;
    var i = _a.i, k = _a.k, stages = _a.stages, totals = _a.totals, maxTotal = _a.maxTotal, funnelWidth = _a.funnelWidth, funnelHeight = _a.funnelHeight;
    var segmentWidth = funnelWidth / stages.length;
    var cur = stages[i];
    var next = stages[i + 1] || { subValues: [] };
    var curTotal = totals[i] || 1;
    var nextTotal = totals[i + 1] || 0;
    var cumTop = 0;
    var cumBot = 0;
    var _loop_2 = function (idx) {
        var v_2 = cur.subValues[idx];
        var vNext_2 = (_b = next.subValues) === null || _b === void 0 ? void 0 : _b.find(function (x) { return x.category === v_2.category; });
        var val_2 = v_2.value;
        var nextVal_2 = vNext_2 ? vNext_2.value : 0;
        cumTop += (val_2 / curTotal) * (curTotal / maxTotal) * funnelHeight;
        cumBot += (nextVal_2 / nextTotal || 0) * (nextTotal / maxTotal) * funnelHeight;
    };
    for (var idx = 0; idx < k; idx++) {
        _loop_2(idx);
    }
    var v = cur.subValues[k];
    var vNext = (_c = next.subValues) === null || _c === void 0 ? void 0 : _c.find(function (x) { return x.category === v.category; });
    var val = v.value;
    var nextVal = vNext ? vNext.value : 0;
    var topH = (val / curTotal) * (curTotal / maxTotal) * funnelHeight;
    var botH = (nextVal / nextTotal || 0) * (nextTotal / maxTotal) * funnelHeight;
    var leftStart = i * segmentWidth;
    var leftEnd = (i + 1) * segmentWidth;
    var topStart = (funnelHeight - (curTotal / maxTotal) * funnelHeight) / 2 + cumTop;
    var topEnd = topStart + topH;
    var botStart = (funnelHeight - (nextTotal / maxTotal) * funnelHeight) / 2 + cumBot;
    var botEnd = botStart + botH;
    var isLastSegment = i === stages.length - 1;
    var textX;
    var textY;
    var availableWidth;
    if (isLastSegment) {
        textX = leftStart + (leftEnd - leftStart) * 0.25;
        textY = (topStart + topEnd) / 2;
        // For triangular segments, calculate available width at text position
        var segmentWidthAtTextPos = (leftEnd - leftStart) * 0.5;
        availableWidth = segmentWidthAtTextPos * 0.8;
        // For triangular last segments, also check if there's enough height
        // The segment area should be large enough to contain text
        var segmentArea = (topH * segmentWidth) / 2;
        if (topH < 24 || segmentArea < 600) {
            availableWidth = 0;
        }
    }
    else {
        textX = (leftStart + leftEnd) / 2;
        textY = (topStart + topEnd + botStart + botEnd) / 4;
        // For trapezoidal segments, use full segment width
        availableWidth = Math.abs(leftEnd - leftStart) * 0.9;
        // Check if the segment has sufficient height for text
        // For non-last segments, we need to ensure there's enough vertical space
        var avgHeight = (topH + botH) / 2;
        if (avgHeight < 20) {
            availableWidth = 0;
        }
    }
    var pathD = "M".concat(leftStart, ",").concat(topStart, "\n    L").concat(leftEnd, ",").concat(botStart, "\n    L").concat(leftEnd, ",").concat(botEnd, "\n    L").concat(leftStart, ",").concat(topEnd, "\n    Z");
    return { pathD: pathD, textX: textX, textY: textY, availableWidth: availableWidth };
}
export function getSegmentTextProps(_a) {
    var availableWidth = _a.availableWidth, _b = _a.minTextWidth, minTextWidth = _b === void 0 ? 24 : _b, textX = _a.textX, textY = _a.textY, value = _a.value, culture = _a.culture, onMouseOver = _a.onMouseOver, onMouseMove = _a.onMouseMove, onMouseOut = _a.onMouseOut;
    return {
        show: availableWidth > minTextWidth && availableWidth > 0,
        x: textX,
        y: textY,
        value: value,
        culture: culture,
        onMouseOver: onMouseOver,
        onMouseMove: onMouseMove,
        onMouseOut: onMouseOut,
    };
}
//# sourceMappingURL=funnelGeometry.js.map