var _uuid, _defaultWidth, _defaultHeight, _isViewInit, _chart, _interval$, _init, _updateData;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { Component, Input } from '@angular/core';
import { ZuiF, ZuiE } from '@material-zui/core';
import { Chart, registerables } from 'chart.js';
import { interval, Subscription } from 'rxjs';
export class ZuiChartComponent {
    constructor() {
        _uuid.set(this, ZuiF.uuid());
        this.id = __classPrivateFieldGet(this, _uuid);
        _defaultWidth.set(this, 'auto');
        this.width = __classPrivateFieldGet(this, _defaultWidth);
        _defaultHeight.set(this, 'auto');
        this.height = __classPrivateFieldGet(this, _defaultHeight);
        _isViewInit.set(this, false);
        _chart.set(this, void 0);
        _interval$.set(this, Subscription.EMPTY);
        _init.set(this, () => {
            var _a;
            const { refresh } = this.config;
            __classPrivateFieldGet(this, _updateData).call(this);
            (_a = __classPrivateFieldGet(this, _interval$)) === null || _a === void 0 ? void 0 : _a.unsubscribe();
            if (refresh) {
                const { time, onChange } = refresh;
                if (!time)
                    throw ZuiE.error({ time });
                if (!onChange)
                    throw ZuiE.error({ onChange });
                __classPrivateFieldSet(this, _interval$, interval(time).subscribe(timeExecute => {
                    const newConfig = onChange(this.config, timeExecute);
                    this.config = Object.assign(Object.assign({}, this.config), newConfig);
                    // console.log('interval', this.config)
                    __classPrivateFieldGet(this, _updateData).call(this);
                }));
            }
        });
        _updateData.set(this, () => {
            var _a;
            (_a = __classPrivateFieldGet(this, _chart)) === null || _a === void 0 ? void 0 : _a.destroy();
            __classPrivateFieldSet(this, _chart, new Chart(this.id, this.config));
        });
        Chart.register(...registerables);
    }
    set setId(id) {
        this.id = id !== null && id !== void 0 ? id : __classPrivateFieldGet(this, _uuid);
    }
    set setWidth(value) {
        this.width = value !== null && value !== void 0 ? value : __classPrivateFieldGet(this, _defaultWidth);
    }
    set setHeight(value) {
        this.height = value !== null && value !== void 0 ? value : __classPrivateFieldGet(this, _defaultHeight);
    }
    ngOnChanges() {
        if (__classPrivateFieldGet(this, _isViewInit))
            __classPrivateFieldGet(this, _init).call(this);
    }
    ngAfterViewInit() {
        __classPrivateFieldSet(this, _isViewInit, true);
        __classPrivateFieldGet(this, _init).call(this);
    }
    ngOnDestroy() {
        var _a;
        (_a = __classPrivateFieldGet(this, _interval$)) === null || _a === void 0 ? void 0 : _a.unsubscribe();
        console.log('destroy');
    }
}
_uuid = new WeakMap(), _defaultWidth = new WeakMap(), _defaultHeight = new WeakMap(), _isViewInit = new WeakMap(), _chart = new WeakMap(), _interval$ = new WeakMap(), _init = new WeakMap(), _updateData = new WeakMap();
ZuiChartComponent.decorators = [
    { type: Component, args: [{
                selector: 'zui-chart',
                template: "<canvas *ngIf=\"config\" [id]=\"id\" [ngStyle]=\"{maxWidth: width, maxHeight: height}\"></canvas>\n",
                styles: [""]
            },] }
];
ZuiChartComponent.ctorParameters = () => [];
ZuiChartComponent.propDecorators = {
    setId: [{ type: Input, args: ['id',] }],
    setWidth: [{ type: Input, args: ['width',] }],
    setHeight: [{ type: Input, args: ['height',] }],
    config: [{ type: Input }]
};
//# sourceMappingURL=index.component.js.map