"use strict";

import { head, last } from "../utils";

/**
 * Bar width is based on the amount of items in the plot data and the distance between the first and last of those
 * items.
 * @param props the props passed to the series.
 * @param moreProps an object holding the xScale, xAccessor and plotData.
 * @return {number} the bar width.
 */
export function plotDataLengthBarWidth(props, moreProps) {
	const { widthRatio: widthRatio = 0.8 } = props;
	const { xScale: scale, xAccessor: accessor, plotData } = moreProps;

	const width = Math.abs((scale(accessor(last(plotData))) - scale(accessor(head(plotData)))) / (plotData.length - 1));
	return width * widthRatio;
}

/**
 * Generates a width function that calculates the bar width based on the given time interval.
 * @param interval a d3-time time interval.
 * @return {Function} the width function.
 */
export function timeIntervalBarWidth(interval) {
	return function(props, moreProps) {
		const { widthRatio: widthRatio = 0.8 } = props;
		const { xScale: scale, xAccessor: accessor, plotData } = moreProps;

		const first = accessor(head(plotData));
		return Math.abs(scale(interval.offset(first, 1)) - scale(first)) * widthRatio;
	};
}

@@ -25,14 +25,8 @@
 head
-, last
  %7D f
@@ -410,34 +410,16 @@
 dthRatio
-: widthRatio = 0.8
  %7D = pro
@@ -441,70 +441,64 @@
 cale
-: scale, xAccessor: accessor, plotData %7D = moreProps;%0A
+ %7D = moreProps;%0A%0A%09const %5Bl, r%5D = xScale.range();
 %0A%09const 
 widt
@@ -493,17 +493,22 @@
 %0A%09const 
-w
+totalW
 idth = M
@@ -519,99 +519,96 @@
 abs(
-(scale(accessor(last(plotData))) - scale(accessor(head(plotData)))) / (plotData.length - 1)
+r - l);%0A%09const %5Bdl, dr%5D = xScale.domain();%0A%0A%09const width = totalWidth / Math.abs(dl - dr
 );%0A%09
@@ -935,26 +935,8 @@
 atio
-: widthRatio = 0.8
  %7D =
@@ -963,15 +963,8 @@
 cale
-: scale
 , xA
@@ -974,18 +974,8 @@
 ssor
-: accessor
 , pl
@@ -1013,17 +1013,18 @@
 first = 
-a
+xA
 ccessor(
@@ -1058,17 +1058,18 @@
 ath.abs(
-s
+xS
 cale(int
@@ -1094,17 +1094,18 @@
 , 1)) - 
-s
+xS
 cale(fir

Modify barWidth to make width not change with there are fewer bars