import { WidgetModel, } from '@jupyter-widgets/base';
import { MODULE_NAME, MODULE_VERSION } from '../version';
class TitleModel extends WidgetModel {
    defaults() {
        return {
            ...super.defaults(),
            _model_name: TitleModel.model_name,
            _model_module: TitleModel.model_module,
            _model_module_version: TitleModel.model_module_version
        };
    }
    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 (TitleModel.updateManager) {
            TitleModel.updateManager.registerModel(this);
            (_a = this.comm) === null || _a === void 0 ? void 0 : _a.on_close(() => {
                TitleModel.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 = TitleModel.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 = TitleModel.updateManager) === null || _a === void 0 ? void 0 : _a.registerChildModel({
                            child: it,
                            parent: m
                        });
                    }
                }
            });
        }
        if (TitleModel.updateManager) {
            TitleModel.updateManager.sendUpdateSignal(this);
            (_a = this.comm) === null || _a === void 0 ? void 0 : _a.on_close(() => {
                TitleModel.updateManager.unregisterModel(this);
            });
        }
    }
}
TitleModel.serializers = {
    ...WidgetModel.serializers,
};
TitleModel.updateManager = null;
TitleModel.model_name = 'TitleModel';
TitleModel.model_module = MODULE_NAME;
TitleModel.model_module_version = MODULE_VERSION;
export { TitleModel };
