/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ReactRenderer, ReactWrapper } from "igniteui-react-core/ES2015/react-renderer";
import { TypeRegistrar } from "igniteui-react-core/ES2015/type";
import { CategoryChart } from './CategoryChart';
import { CategoryChartType_$type } from './CategoryChartType';
import { CategorySeriesMarkerCollisionAvoidance_$type } from './CategorySeriesMarkerCollisionAvoidance';
import { CategoryTransitionInMode_$type } from './CategoryTransitionInMode';
import { TransitionInSpeedType_$type } from './TransitionInSpeedType';
import { fromBrushCollection, toBrushCollection, ensureEnum, ensureBool, initializePropertiesFromCss } from "igniteui-react-core/ES2015/componentUtil";
import { IgrXYChart } from './igr-xy-chart';
import { DataChartStylingDefaults } from './DataChartStylingDefaults';
import { DataSeriesType } from "igniteui-react-core/ES2015/DataSeriesType";
import { DataContext } from "igniteui-react-core/ES2015/igr-data-context";
import { IgrCategoryXAxis } from './igr-category-x-axis';
import { IgrNumericYAxis } from './igr-numeric-y-axis';
export const IgxCategoryChartComponent_PROVIDERS = [];
/**
 * Represents a chart with an ordinal X-axis and a numeric Y-axis.
*/
export class IgrCategoryChart extends IgrXYChart {
    constructor(props) {
        super(props);
        this._dataSource = null;
        this._defaultTooltips = null;
        this._uniqueTooltipId = 0;
        this._getMainRef = this._getMainRef.bind(this);
        this._tooltipRef = this._tooltipRef.bind(this);
        this._activeTooltipElements = new Map();
        this._activeTooltips = new Map();
        this._currentTooltips = [];
        if (document) {
            this._container = document.createElement("div");
            this._container.style.display = "block";
            this._container.style.width = "100%";
            this._container.style.height = "100%";
        }
        var root;
        root = this._container;
        if (this._container != null) {
            root = this._container;
        }
        var ren = new ReactRenderer(root, document, true, DataChartStylingDefaults);
        this._wrapper = ren;
        var chart = this.i;
        this._chart = chart;
        if (chart.xAxis) {
            let x = new IgrCategoryXAxis(null);
            x._provideRenderer(ren);
            x._implementation = chart.xAxis;
            chart.xAxis.externalObject = x;
        }
        if (chart.yAxis) {
            let y = new IgrNumericYAxis(null);
            y._provideRenderer(ren);
            y._implementation = chart.yAxis;
            chart.yAxis.externalObject = y;
        }
        chart.provideContainer(ren);
        this.bindData();
        chart.notifyResized();
        ren.addSizeWatcher(() => {
            this._chart.notifyResized();
        });
        this._initialized = true;
    }
    set height(value) {
        this._height = value;
        if (this._elRef) {
            this._elRef.style.height = value;
            this._chart.notifyResized();
        }
    }
    get height() {
        return this._height;
    }
    set width(value) {
        this._width = value;
        if (this._elRef) {
            this._elRef.style.height = value;
            this._chart.notifyResized();
        }
    }
    get width() {
        return this._width;
    }
    _tooltipRef(t) {
        console.log(t);
        if (t === null) {
            return;
        }
        if (t.currentOwner &&
            t.currentOwner.tooltipTemplate) {
            t.template = t.currentOwner.tooltipTemplate;
        }
        this._activeTooltips.set(t.currentOwner, t);
    }
    _getMainRef(ref) {
        this._elRef = ref;
    }
    render() {
        // if (!this._childrenDiffer(this.props.children)) {
        // 	let div = React.createElement("div", {
        // 		ref: (ref) => {
        // 			this._elRef = ref;
        // 		},
        // 		children: this.props.children
        // 	});
        // 	return div;
        // } else {
        let children = [];
        if (this._currentTooltips && this._currentTooltips.length > 0) {
            for (let i = 0; i < this._currentTooltips.length; i++) {
                let t = this._currentTooltips[i];
                if (this._activeTooltipElements.has(t)) {
                    children.push(this._activeTooltipElements.get(t));
                }
                else {
                    if (!TypeRegistrar.isRegistered("IgrTooltipContainer")) {
                        continue;
                    }
                    let Tooltip = TypeRegistrar.get("IgrTooltipContainer");
                    let tEle = React.createElement(Tooltip, {
                        ref: this._tooltipRef,
                        key: this._currentTooltips[i].key,
                        owner: this._currentTooltips[i]
                    });
                    let portal = ReactDOM.createPortal(tEle, t, this._currentTooltips[i].key);
                    this._activeTooltipElements.set(t, portal);
                    children.push(portal);
                }
            }
        }
        let div = React.createElement("div", {
            ref: this._getMainRef,
            children: children
        });
        return div;
        //}
    }
    componentDidMount() {
        super.componentDidMount();
        this._elRef.style.width = this._width ? this._width : "";
        this._elRef.style.height = this._height ? this._height : "";
        this._elRef.appendChild(this._container);
        this._chart.notifyResized();
        this.initializeContent();
    }
    componentWillUnmount() {
        this._chart.destroy();
        this._wrapper.destroy();
    }
    createImplementation() {
        return new CategoryChart();
    }
    get i() {
        return this._implementation;
    }
    createSeriesComponent(type) {
        if (TypeRegistrar.isRegistered(type)) {
            let s = TypeRegistrar.create(type);
            s.owner = this;
            s._provideRenderer(this._wrapper);
            return s;
        }
        else {
            //we shouldn't get here, hopefully.
            throw Error("series type not loaded: " + type);
        }
    }
    createSeries(type) {
        switch (type) {
            case DataSeriesType.Area:
                return this.createSeriesComponent('IgrAreaSeries');
            case DataSeriesType.Column:
                return this.createSeriesComponent('IgrColumnSeries');
            case DataSeriesType.Line:
                return this.createSeriesComponent('IgrLineSeries');
            case DataSeriesType.Point:
                return this.createSeriesComponent('IgrPointSeries');
            case DataSeriesType.Spline:
                return this.createSeriesComponent('IgrSplineSeries');
            case DataSeriesType.SplineArea:
                return this.createSeriesComponent('IgrSplineAreaSeries');
            case DataSeriesType.StepArea:
                return this.createSeriesComponent('IgrStepAreaSeries');
            case DataSeriesType.StepLine:
                return this.createSeriesComponent('IgrStepLineSeries');
            case DataSeriesType.Waterfall:
                return this.createSeriesComponent('IgrWaterfallSeries');
            default:
                return this.createSeriesComponent('IgrColumnSeries');
        }
    }
    set dataSource(value) {
        this._dataSource = value;
        this.bindData();
    }
    get dataSource() {
        return this._dataSource;
    }
    bindData() {
        if (this._chart != null && this._chart !== undefined) {
            this._chart.itemsSource = this._dataSource;
        }
    }
    initializeContent() {
        if (TypeRegistrar.isRegistered("IgrDataChartDefaultTooltips")) {
            let d = TypeRegistrar.create("IgrDataChartDefaultTooltips");
            this._defaultTooltips = d;
            this._onDefaultTooltipsReady(d);
            //this._container.appendChild(cr.location.nativeElement);
        }
        // supports angular themes or custom properties set in CSS 
        initializePropertiesFromCss(this._elRef, this, "category-chart-");
        this._chart.notifyResized();
    }
    createTooltip() {
        // if (!TypeRegistrar.isRegistered("IgrTooltipContainer")) {
        // 	return null;
        // }
        // let Tooltip = TypeRegistrar.get("IgrTooltipContainer");
        let wrapper = this._wrapper.createElement("div");
        let ele = wrapper.getNativeElement();
        ele.key = "__tooltip_" + this._uniqueTooltipId;
        this._uniqueTooltipId++;
        this._currentTooltips = this._currentTooltips.slice(0);
        this._currentTooltips.push(ele);
        //let element = React.createElement(Tooltip,  );
        //let portal = ReactDOM.createPortal(element, ele);
        let self = this;
        ele.updateToolTip = function (c) {
            if (c.externalObject) {
                c = c.externalObject;
            }
            else {
                let ext = new DataContext();
                ext._implementation = c;
                c = ext;
            }
            if (ele.parentElement != self._container) {
                if (ele.parentElement != null) {
                    ele.parentElement.removeChild(ele);
                }
                self._container.appendChild(ele);
            }
            if (self._activeTooltips.has(ele)) {
                let t = self._activeTooltips.get(ele);
                t.dataContext = c;
            }
            ele.style.display = "block";
            return true;
        };
        ele.hideToolTip = function () {
            ele.style.display = "none";
        };
        ele.style.display = "none";
        this._updateTooltipState();
        return ele;
    }
    _ensureTooltipCreated(series) {
        series._ensureTooltipCreated(() => this.createTooltip(), (ele) => {
            let wrapper = new ReactWrapper(ele, this._wrapper);
            wrapper.updateToolTip = ele.updateToolTip;
            wrapper.hideToolTip = ele.hideToolTip;
            return wrapper;
        });
    }
    _createXAxis() {
        let x = new IgrCategoryXAxis(null);
        x._provideRenderer(this._wrapper);
        return x.i;
    }
    _createYAxis() {
        let y = new IgrNumericYAxis(null);
        y.provideRenderer(this._wrapper);
        return y.i;
    }
    _ensureDefaultTooltip(series) {
        if (this._defaultTooltips == null) {
            return;
        }
        this._defaultTooltips["ensureDefaultTooltip"](series);
    }
    _onDefaultTooltipsReady(cr) {
        if (this.i.dataChart) {
            var currSeries = this.i.dataChart.series;
            for (var i = 0; i < currSeries.count; i++) {
                if (currSeries.item(i).externalObject.showDefaultTooltip) {
                    this._ensureDefaultTooltip(currSeries.item(i).externalObject);
                }
            }
        }
    }
    _updateTooltipState() {
        if (this._initialized) {
            this.setState({ tooltips: this._currentTooltips });
        }
    }
    /**
 * Gets or sets the duration used for animating series plots when the chart is loading into view
*/
    get transitionInDuration() {
        return this.i.oh;
    }
    set transitionInDuration(v) {
        this.i.oh = +v;
    }
    /**
     * Gets or sets the easing function used for animating series plots when the chart is loading into view
     * This can be set to one of the known values "linear" or "cubic," or it can be set to an easing function which takes a single numeric parameter and returns a number.
    */
    get transitionInEasingFunction() {
        return this.i.ni;
    }
    set transitionInEasingFunction(v) {
        this.i.ni = v;
    }
    /**
     * Gets or sets the type of chart series to generate from the data.
    */
    get chartType() {
        return this.i.m3;
    }
    set chartType(v) {
        this.i.m3 = ensureEnum(CategoryChartType_$type, v);
    }
    /**
     * Gets or sets the behavior for markers in each series which are placed too close together for the current view, resulting in a collision.
    */
    get markerCollisionAvoidance() {
        return this.i.m5;
    }
    set markerCollisionAvoidance(v) {
        this.i.m5 = ensureEnum(CategorySeriesMarkerCollisionAvoidance_$type, v);
    }
    /**
     * Gets or sets whether animation of series plots is enabled when the chart is loading into view
    */
    get isTransitionInEnabled() {
        return this.i.no;
    }
    set isTransitionInEnabled(v) {
        this.i.no = ensureBool(v);
    }
    /**
     * Gets or sets the method that determines how to animate series plots when the chart is loading into view
    */
    get transitionInMode() {
        return this.i.m7;
    }
    set transitionInMode(v) {
        this.i.m7 = ensureEnum(CategoryTransitionInMode_$type, v);
    }
    /**
     * Gets or sets the arrival speed used for animating series plots when the chart is loading into view
    */
    get transitionInSpeedType() {
        return this.i.ng;
    }
    set transitionInSpeedType(v) {
        this.i.ng = ensureEnum(TransitionInSpeedType_$type, v);
    }
    /**
     * Gets or sets the frequency of displayed labels along the X-axis.
     * Gets or sets the set value is a factor that determines which labels will be hidden. For example, an interval of 2 will display every other label.
    */
    get xAxisInterval() {
        return this.i.n6;
    }
    set xAxisInterval(v) {
        this.i.n6 = +v;
    }
    /**
     * Gets or sets the frequency of displayed minor lines along the X-axis.
     * Gets or sets the set value is a factor that determines how the minor lines will be displayed.
    */
    get xAxisMinorInterval() {
        return this.i.n7;
    }
    set xAxisMinorInterval(v) {
        this.i.n7 = +v;
    }
    /**
     * Gets or sets the amount of space between adjacent categories for the X-axis.
     * The gap is silently clamped to the range [0, 1] when used.
    */
    get xAxisGap() {
        return this.i.n5;
    }
    set xAxisGap(v) {
        this.i.n5 = +v;
    }
    /**
     * Gets or sets the amount of overlap between adjacent categories for the X-axis.
     * Gets or sets the overlap is silently clamped to the range [-1, 1] when used.
    */
    get xAxisOverlap() {
        return this.i.n8;
    }
    set xAxisOverlap(v) {
        this.i.n8 = +v;
    }
    /**
     * Gets or sets the distance between each label and grid line along the Y-axis.
    */
    get yAxisInterval() {
        return this.i.ob;
    }
    set yAxisInterval(v) {
        this.i.ob = +v;
    }
    /**
     * Gets or sets whether the Y-axis should use a logarithmic scale instead of a linear one.
     * Since log(-1) is imaginary and log(0) is undefined, it is recommended to enable this property only when the Y-axis minimum is greater than zero.
    */
    get yAxisIsLogarithmic() {
        return this.i.nq;
    }
    set yAxisIsLogarithmic(v) {
        this.i.nq = ensureBool(v);
    }
    /**
     * Gets or sets the base value to use in the log function when mapping the position of data items along the Y-axis.
     * This property is effective only when YAxisIsLogarithmic is true.
    */
    get yAxisLogarithmBase() {
        return this.i.oi;
    }
    set yAxisLogarithmBase(v) {
        this.i.oi = +v;
    }
    /**
     * Gets or sets the data value corresponding to the minimum value of the Y-axis.
    */
    get yAxisMinimumValue() {
        return this.i.od;
    }
    set yAxisMinimumValue(v) {
        this.i.od = +v;
    }
    /**
     * Gets or sets the data value corresponding to the maximum value of the Y-axis.
    */
    get yAxisMaximumValue() {
        return this.i.oc;
    }
    set yAxisMaximumValue(v) {
        this.i.oc = +v;
    }
    /**
     * Gets or sets the frequency of displayed minor lines along the Y-axis.
    */
    get yAxisMinorInterval() {
        return this.i.oe;
    }
    set yAxisMinorInterval(v) {
        this.i.oe = +v;
    }
    /**
     * Gets the current minimum value for the X-axis.
    */
    get xAxisActualMinimum() {
        return this.i.n4;
    }
    /**
     * Gets the current maximum value for the X-axis.
    */
    get xAxisActualMaximum() {
        return this.i.n3;
    }
    /**
     * Gets the current minimum value for the Y-axis.
    */
    get yAxisActualMinimum() {
        return this.i.oa;
    }
    /**
     * Gets the current maximum value for the Y-axis.
    */
    get yAxisActualMaximum() {
        return this.i.n9;
    }
    /**
     * Gets or sets the palette used for coloring negative items of Waterfall chart type.
    */
    get negativeBrushes() {
        return fromBrushCollection(this.i.m0);
    }
    set negativeBrushes(v) {
        this.i.m0 = toBrushCollection(v);
    }
    /**
     * Brushes to use for drawing negative elements, when using a chart type with contextual coloring, such as Waterfall.
    */
    get negativeOutlines() {
        return fromBrushCollection(this.i.m1);
    }
    set negativeOutlines(v) {
        this.i.m1 = toBrushCollection(v);
    }
    /**
     * Gets or sets whether the series animations should be allowed when a range change has been detected on an axis.
    */
    get animateSeriesWhenAxisRangeChanges() {
        return this.i.nn;
    }
    set animateSeriesWhenAxisRangeChanges(v) {
        this.i.nn = ensureBool(v);
    }
    /**
     * Gets or sets whether the large numbers on the Y-axis labels are abbreviated.
    */
    get yAxisAbbreviateLargeNumbers() {
        return this.i.np;
    }
    set yAxisAbbreviateLargeNumbers(v) {
        this.i.np = ensureBool(v);
    }
    /**
 * Export serialized visual data.

*/
    exportSerializedVisualData() {
        let iv = this.i.ol();
        return (iv);
    }
    /**
     * Converts the given visual location to a data value.
    
    * @param unscaledValue  * The x-coordinate of the location to scale.
    */
    getScaledValueX(unscaledValue) {
        let iv = this.i.nz(unscaledValue);
        return (iv);
    }
    /**
     * Converts the given data value to a visual location.
    
    * @param scaledValue  * The data value to unscale.
    */
    getUnscaledValueX(scaledValue) {
        let iv = this.i.n1(scaledValue);
        return (iv);
    }
    /**
     * Converts the given visual location to a data value.
    
    * @param unscaledValue  * The y-coordinate of the location to scale.
    */
    getScaledValueY(unscaledValue) {
        let iv = this.i.n0(unscaledValue);
        return (iv);
    }
    /**
     * Converts the given data value to a visual location.
    
    * @param scaledValue  * The data value to unscale.
    */
    getUnscaledValueY(scaledValue) {
        let iv = this.i.n2(scaledValue);
        return (iv);
    }
}
