import { WidgetModel, unpack_models } from '@jupyter-widgets/base';
import { MODULE_NAME, MODULE_VERSION } from '../version';
class OptionModel extends WidgetModel {
    defaults() {
        return {
            ...super.defaults(),
            _model_name: OptionModel.model_name,
            _model_module: OptionModel.model_module,
            _model_module_version: OptionModel.model_module_version,
            title: undefined,
            legend: undefined,
            grid: undefined,
            xAxis: undefined,
            yAxis: undefined,
            polar: undefined,
            radiusAxis: undefined,
            angleAxis: undefined,
            radar: undefined,
            tooltip: undefined,
            axisPointer: undefined,
            toolbox: undefined,
            brush: undefined,
            geo: undefined,
            parallel: undefined,
            parallelAxis: undefined,
            singleAxis: undefined,
            timeline: undefined,
            graphic: undefined,
            calendar: undefined,
            matrix: undefined,
            thumbnail: undefined,
            dataset: undefined,
            aria: undefined,
            color: undefined,
            textStyle: undefined,
            stateAnimation: undefined,
            options: undefined,
            globe: undefined,
            geo3D: undefined,
            mapbox3D: undefined,
            grid3D: undefined,
            xAxis3D: undefined,
            yAxis3D: undefined,
            zAxis3D: undefined,
            series: undefined
        };
    }
    toDict() {
        const dict = {};
        for (const [key, val] of Object.entries(this.attributes)) {
            if (key === 'unknown_attrs') {
                if (val) {
                    for (const [unknown_key, unknown_val] of Object.entries(val)) {
                        dict[unknown_key] = unknown_val;
                    }
                }
                continue;
            }
            if (typeof val === 'string' && val.startsWith('__ipecharts_jsfn__::')) {
                const funcStr = val.replace('__ipecharts_jsfn__::', '');
                const decodedString = atob(funcStr);
                const jsonObject = JSON.parse(decodedString);
                const fn = new Function(...jsonObject);
                dict[key] = fn;
                continue;
            }
            if (val !== undefined && val !== null && !key.startsWith('_')) {
                dict[key] = val;
            }
        }
        return dict;
    }
    initialize(attributes, options) {
        var _a;
        super.initialize(attributes, options);
        if (OptionModel.updateManager) {
            OptionModel.updateManager.registerModel(this);
            (_a = this.comm) === null || _a === void 0 ? void 0 : _a.on_close(() => {
                OptionModel.updateManager.unregisterModel(this);
            });
        }
        this.listenTo(this, 'change', this.valueChanged);
    }
    valueChanged(m) {
        var _a;
        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 = OptionModel.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 = OptionModel.updateManager) === null || _a === void 0 ? void 0 : _a.registerChildModel({
                            child: it,
                            parent: m
                        });
                    }
                }
            });
        }
        if (OptionModel.updateManager) {
            OptionModel.updateManager.sendUpdateSignal(this);
            (_a = this.comm) === null || _a === void 0 ? void 0 : _a.on_close(() => {
                OptionModel.updateManager.unregisterModel(this);
            });
        }
    }
}
OptionModel.serializers = {
    ...WidgetModel.serializers,
    title: { deserialize: unpack_models },
    legend: { deserialize: unpack_models },
    grid: { deserialize: unpack_models },
    xAxis: { deserialize: unpack_models },
    yAxis: { deserialize: unpack_models },
    polar: { deserialize: unpack_models },
    radiusAxis: { deserialize: unpack_models },
    angleAxis: { deserialize: unpack_models },
    radar: { deserialize: unpack_models },
    tooltip: { deserialize: unpack_models },
    axisPointer: { deserialize: unpack_models },
    toolbox: { deserialize: unpack_models },
    brush: { deserialize: unpack_models },
    geo: { deserialize: unpack_models },
    parallel: { deserialize: unpack_models },
    parallelAxis: { deserialize: unpack_models },
    singleAxis: { deserialize: unpack_models },
    timeline: { deserialize: unpack_models },
    graphic: { deserialize: unpack_models },
    calendar: { deserialize: unpack_models },
    matrix: { deserialize: unpack_models },
    thumbnail: { deserialize: unpack_models },
    dataset: { deserialize: unpack_models },
    aria: { deserialize: unpack_models },
    textStyle: { deserialize: unpack_models },
    stateAnimation: { deserialize: unpack_models },
    globe: { deserialize: unpack_models },
    geo3D: { deserialize: unpack_models },
    mapbox3D: { deserialize: unpack_models },
    grid3D: { deserialize: unpack_models },
    xAxis3D: { deserialize: unpack_models },
    yAxis3D: { deserialize: unpack_models },
    zAxis3D: { deserialize: unpack_models },
    series: { deserialize: unpack_models }
};
OptionModel.updateManager = null;
OptionModel.model_name = 'OptionModel';
OptionModel.model_module = MODULE_NAME;
OptionModel.model_module_version = MODULE_VERSION;
export { OptionModel };
