import * as React from 'react';
import { ReactRenderer } from "igniteui-react-core/ES2015/react-renderer";
import { TypeRegistrar } from "igniteui-react-core/ES2015/type";
import { IgrPieChartBase } from './igr-pie-chart-base';
import { DataChartStylingDefaults } from './DataChartStylingDefaults';
import { XamPieChart } from './XamPieChart';
export class IgrPieChart extends IgrPieChartBase {
    constructor(props) {
        super(props);
        this._dataSource = null;
        this._getMainRef = this._getMainRef.bind(this);
        let container;
        if (document) {
            container = document.createElement("div");
            container.style.display = "block";
            container.style.width = "100%";
            container.style.height = "100%";
        }
        var root;
        root = container;
        var ren = new ReactRenderer(root, document, true, DataChartStylingDefaults);
        this.container = ren.getWrapper(container);
        this._wrapper = ren;
        var chart = this.i;
        this._chart = chart;
        chart.provideContainer(ren);
        this.bindData();
        chart.notifyContainerResized();
        ren.addSizeWatcher(() => {
            this._chart.notifyContainerResized();
        });
    }
    set height(value) {
        this._height = value;
        if (this._elRef) {
            this._elRef.style.height = value;
            this._chart.notifyContainerResized();
        }
    }
    get height() {
        return this._height;
    }
    set width(value) {
        this._width = value;
        if (this._elRef) {
            this._elRef.style.width = value;
            this._chart.notifyContainerResized();
        }
    }
    get width() {
        return this._width;
    }
    _getMainRef(ref) {
        this._elRef = ref;
    }
    render() {
        let div = React.createElement("div", {
            ref: this._getMainRef
        });
        return div;
    }
    destroy() {
        this._chart.destroy();
        this._wrapper.destroy();
    }
    componentDidMount() {
        super.componentDidMount();
        this._elRef.style.width = this._width ? this._width : "";
        this._elRef.style.height = this._height ? this._height : "";
        this._elRef.appendChild(this.container.getNativeElement());
        this._chart.notifyContainerResized();
    }
    componentWillUnmount() {
        this.destroy();
    }
    createImplementation() {
        return new XamPieChart();
    }
    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);
        }
    }
    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;
        }
    }
    /**
     * Gets or sets the legend used for the current chart.
    */
    get legend() {
        if (this.i.legend != null)
            return this.i.legend.externalObject;
    }
    set legend(v) {
        if (v != undefined && v != null)
            this.i.legend = v.i;
    }
}
