'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-58ea899e.js');
const legendInputProcessor = require('./legend-input-processor-1e53e5bc.js');
const yAxisInputProcessor = require('./y-axis-input-processor-54a26515.js');
const globalSlimmerRegistry = require('./global-slimmer-registry-b3bce7e0.js');
const componentUsageRuntimeMetrics = require('./component-usage-runtime-metrics-4572c5b7.js');
require('./array-249289f2.js');
require('./number-4816603d.js');
require('./typography-4560e7f1.js');
require('./pixel-98f2a10c.js');

/** The RectDrawer class draws a rectangle on a chart using the provided options. */
class RectDrawer extends legendInputProcessor.BaseDrawer {
  /** This function adds interaction support */
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  addInteractionSupport() { }
  /**
   * This function draws a rectangle on a chart using the provided options.
   */
  draw() {
    const { dataSource = [], xScale, yScale, colors, direction, } = this.options.renderOptions;
    const xData = dataSource.map((item) => item.x);
    const yData = dataSource.map((item) => item.y);
    xData.forEach((item, i) => {
      var _a, _b;
      const yValue = yData[i] || 0;
      let width = 0, height = 0, position = { x: 0, y: 0 };
      if (direction !== 'horizontal') {
        width = ((_a = xScale.bandwidth) === null || _a === void 0 ? void 0 : _a.call(xScale)) || 5;
        height = Math.abs(yScale(0) - yScale(yValue));
        position = { x: xScale(item), y: yScale(Math.max(0, yValue)) };
      }
      else {
        height = ((_b = yScale.bandwidth) === null || _b === void 0 ? void 0 : _b.call(yScale)) || 5;
        width = Math.abs(xScale(yValue) - xScale(0));
        position = { x: xScale(Math.min(yValue, 0)), y: yScale(item) };
      }
      this.renderer.drawRect(position, width, height, this.getColorFromToken(this.getColorByIndex(0, colors)));
    });
  }
}

/** The `HorizontalXAxisInputProcessor` class is a TypeScript class that generates x-axis options for a line chart based on input data and previous layout results. */
class HorizontalXAxisInputProcessor extends yAxisInputProcessor.YAxisInputProcessor {
  /**
   * This function generates the horizontal x-axis options for a line chart based on the input data and previous layout results.
   *
   * @param {ChartOptions<LineChartDataType>} input - An object containing the chart options and data for a line chart. It includes the
   * dataSource and yAxis properties.
   * @param {LayoutProcessResultType} [previousOutput] - The output of the previous layout process,
   * which is an object containing the innerWidth and innerHeight of the chart. This parameter is
   * optional and can be undefined if it's the first layout process.
   * @returns {ChartExtend<AxisScale<AxisDomain>>} an object of type `ChartExtend<AxisScale<AxisDomain>>`, which is the result of calling the
   * `getAxisOptions` method of the `axisGenerator` object. This object contains options for the Y-axis
   * of a line chart, including the scale used for the axis.
   */
  process(input, previousOutput) {
    const { dataSource, yAxis } = input;
    const { innerWidth, innerHeight } = previousOutput;
    const { scale, ticks } = this.getScale(dataSource, [0, innerWidth], 4);
    return {
      start: { x: 0, y: innerHeight },
      end: { x: innerWidth, y: innerHeight },
      tickSize: ticks,
      scale: scale,
      labelFormatter: yAxis === null || yAxis === void 0 ? void 0 : yAxis.labelFormatter,
      width: innerWidth,
      height: innerHeight,
      direction: 'horizontal',
    };
  }
}

/** The `HorizontalYAxisInputProcessor` class is a TypeScript class that generates y-axis options for a line chart based on input data and previous layout results. */
class HorizontalYAxisInputProcessor extends yAxisInputProcessor.XAxisInputProcessor {
  /**
   * This function generates horizontal y-axis options for a line chart based on input data and previous layout results.
   *
   * @param {ChartOptions<LineChartDataType>} input - An object containing chart options, including the data source, x-axis options, and
   * container element.
   * @param {LayoutProcessResultType} [previousOutput] - The optional output from the previous layout
   * process. It is of type LayoutProcessResultType and contains information about the innerWidth and
   * innerHeight of the chart container.
   * @returns {ChartExtend<AxisScale<AxisDomain>>} a ChartExtend object that contains the options for the X-axis of a line chart, including
   * the scale generated by the scaleGenerator and the axis options generated by the axisGenerator.
   */
  process(input, previousOutput) {
    const { dataSource, xAxis, container } = input;
    const { innerWidth, innerHeight } = previousOutput;
    const xAxisType = (xAxis === null || xAxis === void 0 ? void 0 : xAxis.type) || 'value';
    const scale = this.getScale(dataSource, innerHeight, xAxisType, container.tagName === 'VEGA-LINE-CHART' ? 1 : 0.1, xAxis === null || xAxis === void 0 ? void 0 : xAxis.sorter);
    return {
      start: { x: 0, y: 0 },
      end: { x: 0, y: innerHeight },
      tickSize: 4,
      scale: scale,
      labelFormatter: xAxis === null || xAxis === void 0 ? void 0 : xAxis.labelFormatter,
      width: innerWidth,
      height: innerHeight,
      direction: 'horizontal',
    };
  }
}

/**
 * The BarChart class is a TypeScript implementation of a bar chart that extends the BaseChart class
 * and includes methods for creating a drawer chain
 */
class BarChart extends legendInputProcessor.BaseChart {
  constructor() {
    super(...arguments);
    this.layoutInputProcessor = new legendInputProcessor.LayoutInputProcessor();
    this.xAxisInputProcessorChain = new legendInputProcessor.ProcessorChain([this.layoutInputProcessor, new yAxisInputProcessor.XAxisInputProcessor()]);
    this.horizontalXAxisInputProcessorChain = new legendInputProcessor.ProcessorChain([this.layoutInputProcessor, new HorizontalXAxisInputProcessor()]);
    this.yAxisInputProcessorChain = new legendInputProcessor.ProcessorChain([this.layoutInputProcessor, new yAxisInputProcessor.YAxisInputProcessor()]);
    this.horizontalYAxisInputProcessorChain = new legendInputProcessor.ProcessorChain([this.layoutInputProcessor, new HorizontalYAxisInputProcessor()]);
    this.legendInputProcessorChain = new legendInputProcessor.ProcessorChain([
      this.layoutInputProcessor,
      new legendInputProcessor.LegendInputProcessor(),
    ]);
  }
  /**
   * This function creates an array of BaseDrawer objects for rendering a chart with axes, legends, and paths.
   *
   * @param {BaseRenderer<SVGElement | HTMLCanvasElement>} renderer - an instance of a BaseRenderer class that can render SVG or Canvas elements.
   * @param {BaseInteractor<SVGElement | HTMLCanvasElement>} interactor - an instance of a BaseInteractor class
   * @returns {BaseDrawer<unknown>[]} An array of BaseDrawer array.
   */
  createDrawerChain(renderer, interactor) {
    const { colors, xAxis, dataSource, direction } = this.options;
    const renderOptions = Object.assign(Object.assign({}, this.options), { xAxis: Object.assign(Object.assign({}, xAxis), { type: 'category' }) });
    const xAxisOutput = direction === 'horizontal'
      ? this.horizontalXAxisInputProcessorChain.process(renderOptions)
      : this.xAxisInputProcessorChain.process(renderOptions);
    const yAxisOutput = direction === 'horizontal'
      ? this.horizontalYAxisInputProcessorChain.process(renderOptions)
      : this.yAxisInputProcessorChain.process(renderOptions);
    return [
      new yAxisInputProcessor.XAxisDrawer(renderer, interactor, {
        renderOptions: xAxisOutput,
      }),
      new yAxisInputProcessor.YAxisDrawer(renderer, interactor, {
        renderOptions: yAxisOutput,
      }),
      new legendInputProcessor.LegendDrawer(renderer, interactor, {
        renderOptions: this.legendInputProcessorChain.process(renderOptions),
      }),
      new RectDrawer(renderer, interactor, {
        renderOptions: {
          dataSource,
          xScale: xAxisOutput.scale,
          yScale: yAxisOutput.scale,
          colors,
          direction,
        },
      }),
    ];
  }
}

const vegaBarChartCss = ".vega-chart-legend-item{cursor:pointer}.vega-chart-line-path:hover{stroke-width:5}:host{display:block}";

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __rest = (undefined && undefined.__rest) || function (s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
const VegaBarChart = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.globalSlimmers = {};
    this.vegaComponentUsageRuntimeMetricsSlimmer = new componentUsageRuntimeMetrics.VegaComponentUsageRuntimeMetricsSlimmer();
    /**
     * Specifies the rendering options for the bar chart.
     *
     * @vegaVersion 1.33.0
     */
    this.options = {};
  }
  watchOptions(options) {
    this.initChart(options);
  }
  componentWillLoad() {
    this.initChart(this.options);
  }
  render() {
    return index.h(index.Host, null);
  }
  /**
   * This function draws a bar chart using the provided options and data source.
   *
   * @param {LineChartOptionsType} options - The options parameter is an object that contains various options for configuring and rendering a line chart.
   */
  initChart(options) {
    const { mode = 'svg', dataSource = [] } = options, restOptions = __rest(options, ["mode", "dataSource"]);
    const mergedOptions = Object.assign(Object.assign({}, restOptions), { mode, container: this.host, dataSource: dataSource });
    if (this.chart) {
      this.chart.updateOptions(mergedOptions);
    }
    else {
      this.chart = new BarChart(mergedOptions);
      this.chart.render();
    }
  }
  get host() { return index.getElement(this); }
  static get watchers() { return {
    "options": ["watchOptions"]
  }; }
};
__decorate([
  componentUsageRuntimeMetrics.InjectVegaGlobalSlimmer()
], VegaBarChart.prototype, "globalSlimmers", void 0);
__decorate([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaBarChart.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
VegaBarChart.style = vegaBarChartCss;

exports.vega_bar_chart = VegaBarChart;
