"use strict";
// some helper functions to quickly create widgets
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStrokeWidth = exports.getStrokes = exports.getFills = exports.create_figure_image = exports.create_figure_gridheatmap = exports.create_figure_hist = exports.create_figure_bars = exports.create_figure_pie = exports.create_figure_lines = exports.create_figure_scatter = exports.create_widget = exports.create_view = exports.create_model = exports.create_model_bqplot = void 0;
function create_model_bqplot(manager, name, id, args) {
    return __awaiter(this, void 0, void 0, function* () {
        return create_model(manager, 'bqplot', `${name}Model`, name, id, args);
    });
}
exports.create_model_bqplot = create_model_bqplot;
function create_model(manager, module, model, view, id, args = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const model_widget = yield manager.new_widget({
            model_module: module,
            model_name: model,
            model_module_version: '*',
            view_module: module,
            view_name: view,
            view_module_version: '*',
            model_id: id,
        }, args);
        return model_widget;
    });
}
exports.create_model = create_model;
function create_view(manager, model, options = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const view = yield manager.create_view(model, options);
        return view;
    });
}
exports.create_view = create_view;
function create_widget(manager, name, id, args) {
    return __awaiter(this, void 0, void 0, function* () {
        const model = yield create_model_bqplot(manager, name, id, args);
        const view = yield manager.create_view(model);
        yield manager.display_view(undefined, view);
        return { model: model, view: view };
    });
}
exports.create_widget = create_widget;
function create_figure_scatter(manager, x, y, mega = false, log = false) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        let scale_x;
        let scale_y;
        if (log) {
            scale_x = yield create_model_bqplot(manager, 'LogScale', 'scale_x', {
                min: 0.01,
                max: 100,
                allow_padding: false,
            });
            scale_y = yield create_model_bqplot(manager, 'LogScale', 'scale_y', {
                min: 0.1,
                max: 10,
                allow_padding: false,
            });
        }
        else {
            scale_x = yield create_model_bqplot(manager, 'LinearScale', 'scale_x', {
                min: 0,
                max: 1,
                allow_padding: false,
            });
            scale_y = yield create_model_bqplot(manager, 'LinearScale', 'scale_y', {
                min: 2,
                max: 3,
                allow_padding: false,
            });
        }
        // TODO: the default values for the ColorScale should not be required, but defined in the defaults method
        const scale_color = yield create_model_bqplot(manager, 'ColorScale', 'scale_color', { scheme: 'RdYlGn', colors: [] });
        const scales = {
            x: scale_x.toJSON(),
            y: scale_y.toJSON(),
            color: scale_color.toJSON(),
        };
        const color = null;
        const size = null;
        const opacity = null;
        const rotation = null;
        const skew = null;
        const scatterModel = yield create_model_bqplot(manager, mega ? 'ScatterGL' : 'Scatter', 'scatter1', {
            scales: scales,
            x: x,
            y: y,
            color: color,
            size: size,
            opacity: opacity,
            rotation: rotation,
            skew: skew,
            colors: ['steelblue'],
            visible: true,
            default_size: 64,
            selected_style: {},
            unselected_style: {},
            hovered_style: {},
            unhovered_style: {},
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scales['x'],
                scale_y: scales['y'],
                layout: layout.toJSON(),
                _dom_classes: [],
                figure_padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [scatterModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, scatter: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_scatter = create_figure_scatter;
function create_figure_lines(manager, x, y, default_scales = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        const scale_x = yield create_model_bqplot(manager, 'LinearScale', 'scale_x', {
            min: 0,
            max: 1,
            allow_padding: false,
        });
        const scale_y = yield create_model_bqplot(manager, 'LinearScale', 'scale_y', {
            min: 2,
            max: 3,
            allow_padding: false,
        });
        const scales = { x: scale_x.toJSON(), y: scale_y.toJSON() };
        const scales_mark = {
            x: default_scales['x'] || scale_x.toJSON(),
            y: default_scales['y'] || scale_y.toJSON(),
        };
        const color = null;
        const size = null;
        const opacity = null;
        const rotation = null;
        const skew = null;
        const linesModel = yield create_model_bqplot(manager, 'Lines', 'lines1', {
            scales: scales_mark,
            x: x,
            y: y,
            color: color,
            size: size,
            opacity: opacity,
            rotation: rotation,
            skew: skew,
            visible: true,
            default_size: 64,
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scales['x'],
                scale_y: scales['y'],
                layout: layout.toJSON(),
                _dom_classes: [],
                figure_padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [linesModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, lines: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_lines = create_figure_lines;
function create_figure_pie(manager, sizes, labels) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        const scale_x = yield create_model_bqplot(manager, 'LinearScale', 'scale_x', {
            allow_padding: false,
        });
        const scale_y = yield create_model_bqplot(manager, 'LinearScale', 'scale_y', {
            allow_padding: false,
        });
        const pieModel = yield create_model_bqplot(manager, 'Pie', 'pie1', {
            sizes: sizes,
            labels: labels,
            visible: true,
            default_size: 64,
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scale_x.toJSON(),
                scale_y: scale_y.toJSON(),
                layout: layout.toJSON(),
                _dom_classes: [],
                figure_padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [pieModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, pie: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_pie = create_figure_pie;
function create_figure_bars(manager, x, y) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        const scale_x = yield create_model_bqplot(manager, 'LinearScale', 'scale_x', {
            min: 0,
            max: 1,
            allow_padding: false,
        });
        const scale_y = yield create_model_bqplot(manager, 'LinearScale', 'scale_y', {
            min: 0,
            max: 3,
            allow_padding: false,
        });
        const scales = { x: scale_x.toJSON(), y: scale_y.toJSON() };
        const color = null;
        const size = null;
        const opacity = null;
        const rotation = null;
        const skew = null;
        const barsModel = yield create_model_bqplot(manager, 'Bars', 'bars1', {
            scales: scales,
            x: x,
            y: y,
            color: color,
            size: size,
            opacity: opacity,
            rotation: rotation,
            skew: skew,
            visible: true,
            default_size: 64,
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scales['x'],
                scale_y: scales['y'],
                layout: layout.toJSON(),
                _dom_classes: [],
                figure_padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [barsModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, bars: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_bars = create_figure_bars;
function create_figure_hist(manager, sample, bins) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        const scale_sample = yield create_model_bqplot(manager, 'LinearScale', 'scale_sample', { allow_padding: false });
        const scale_count = yield create_model_bqplot(manager, 'LinearScale', 'scale_count', { allow_padding: false });
        const scales = { sample: scale_sample.toJSON(), count: scale_count.toJSON() };
        const histModel = yield create_model_bqplot(manager, 'Hist', 'hist1', {
            scales: scales,
            sample: sample,
            bins: bins,
            visible: true,
            default_size: 64,
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scales.sample,
                scale_y: scales.count,
                layout: layout.toJSON(),
                _dom_classes: [],
                figure_padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [histModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, hist: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_hist = create_figure_hist;
function create_figure_gridheatmap(manager, color) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        const scale_x = yield create_model_bqplot(manager, 'LinearScale', 'scale_x', {
            min: 0,
            max: 1,
            allow_padding: false,
        });
        const scale_y = yield create_model_bqplot(manager, 'LinearScale', 'scale_y', {
            min: 0,
            max: 1,
            allow_padding: false,
        });
        const scale_row = yield create_model_bqplot(manager, 'OrdinalScale', 'scale_row', { reverse: true });
        const scale_column = yield create_model_bqplot(manager, 'OrdinalScale', 'scale_column', {});
        const scale_color = yield create_model_bqplot(manager, 'ColorScale', 'scale_color', { scheme: 'RdYlGn', colors: [] });
        const scales = {
            color: scale_color.toJSON(),
            row: scale_row.toJSON(),
            column: scale_column.toJSON(),
        };
        const gridModel = yield create_model_bqplot(manager, 'GridHeatMap', 'gridheatmap1', {
            scales: scales,
            color: color,
            visible: true,
            default_size: 64,
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scale_x.toJSON(),
                scale_y: scale_y.toJSON(),
                layout: layout.toJSON(),
                _dom_classes: [],
                padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [gridModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, grid: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_gridheatmap = create_figure_gridheatmap;
function create_figure_image(manager, ipywidgetImage) {
    return __awaiter(this, void 0, void 0, function* () {
        const layout = yield create_model(manager, '@jupyter-widgets/base', 'LayoutModel', 'LayoutView', 'layout_figure1', { _dom_classes: '', width: '400px', height: '500px' });
        const scale_x = yield create_model_bqplot(manager, 'LinearScale', 'scale_x', {
            allow_padding: false,
        });
        const scale_y = yield create_model_bqplot(manager, 'LinearScale', 'scale_y', {
            allow_padding: false,
        });
        const scales = { x: scale_x.toJSON(), y: scale_y.toJSON() };
        const imageModel = yield create_model_bqplot(manager, 'Image', 'image1', {
            scales: scales,
            image: ipywidgetImage.toJSON(),
            preserve_domain: {},
            _view_module_version: '*',
            _view_module: 'bqplot',
        });
        let figureModel;
        try {
            figureModel = yield create_model_bqplot(manager, 'Figure', 'figure1', {
                scale_x: scale_x.toJSON(),
                scale_y: scale_y.toJSON(),
                layout: layout.toJSON(),
                _dom_classes: [],
                figure_padding_y: 0,
                fig_margin: { bottom: 0, left: 0, right: 0, top: 0 },
                marks: [imageModel.toJSON()],
            });
        }
        catch (e) {
            console.error('error', e);
        }
        const figure = yield create_view(manager, figureModel);
        yield manager.display_view(undefined, figure);
        yield figure._initial_marks_created;
        return { figure: figure, image: yield figure.mark_views.views[0] };
    });
}
exports.create_figure_image = create_figure_image;
function getFills(selection) {
    return selection.nodes().map((el) => el.style.fill);
}
exports.getFills = getFills;
function getStrokes(selection) {
    return selection.nodes().map((el) => el.style.stroke);
}
exports.getStrokes = getStrokes;
function getStrokeWidth(selection) {
    return selection.nodes().map((el) => el.style.strokeWidth);
}
exports.getStrokeWidth = getStrokeWidth;
//# sourceMappingURL=widget-utils.js.map