// Copyright (c) Trung Le
// Distributed under the terms of the Modified BSD License.
import { BaseEChartsWidgetModel } from './baseWidgetModel';
import { BaseEChartsWidgetView } from './baseWidgetView';
import { unpack_models } from '@jupyter-widgets/base';
class EChartsWidgetModel extends BaseEChartsWidgetModel {
    initialize(attributes, options) {
        super.initialize(attributes, options);
        if (EChartsWidgetModel.updateManager) {
            EChartsWidgetModel.updateManager.registerModel(this);
        }
        this.listenTo(this, 'change', this.valueChanged);
    }
    valueChanged(m) {
        if (m === null || m === void 0 ? void 0 : m.changed) {
            Object.values(m.changed).forEach(it => {
                var _a;
                if (Array.isArray(it)) {
                    it.forEach(subModel => {
                        var _a;
                        if (subModel === null || subModel === void 0 ? void 0 : subModel.model_id) {
                            (_a = EChartsWidgetModel.updateManager) === null || _a === void 0 ? void 0 : _a.registerChildModel({
                                child: subModel,
                                parent: m
                            });
                        }
                    });
                }
                else {
                    if (it === null || it === void 0 ? void 0 : it.model_id) {
                        (_a = EChartsWidgetModel.updateManager) === null || _a === void 0 ? void 0 : _a.registerChildModel({
                            child: it,
                            parent: m
                        });
                    }
                }
            });
        }
    }
}
EChartsWidgetModel.serializers = {
    ...BaseEChartsWidgetModel.serializers,
    option: { deserialize: unpack_models }
};
EChartsWidgetModel.updateManager = null;
EChartsWidgetModel.model_name = 'EChartsWidgetModel';
EChartsWidgetModel.view_name = 'EChartsWidgetView'; // Set to null if no view
export { EChartsWidgetModel };
class EChartsWidgetView extends BaseEChartsWidgetView {
    _createOptionDict() {
        const option = this.model.get('option');
        const optionDict = option.toDict();
        const chartOption = {};
        for (const [key, val] of Object.entries(optionDict)) {
            if (!val) {
                continue;
            }
            if (val.toDict) {
                chartOption[key] = val.toDict();
            }
            else if (Array.isArray(val)) {
                const valArray = val.map(it => (it.toDict ? it.toDict() : it));
                chartOption[key] = valArray;
            }
            else {
                chartOption[key] = val;
            }
        }
        return chartOption;
    }
}
EChartsWidgetView.themeManager = BaseEChartsWidgetView.themeManager;
export { EChartsWidgetView };
