"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
const chai_1 = require("chai");
const d3Timer = __importStar(require("d3-timer"));
const dummy_manager_1 = require("./dummy-manager");
const widget_utils_1 = require("./widget-utils");
const bqplot = __importStar(require(".."));
describe('bars >', () => {
    beforeEach(function () {
        return __awaiter(this, void 0, void 0, function* () {
            this.manager = new dummy_manager_1.DummyManager({ bqplot: bqplot });
        });
    });
    it('create 1d', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 1]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 3]).buffer),
            };
            const objects = yield widget_utils_1.create_figure_bars(this.manager, x, y);
            const bars = objects.bars;
            const data = bars.d3el.selectAll('.bargroup').data();
            chai_1.expect(data[0].values[0].x).to.equal(0);
            chai_1.expect(data[1].values[0].x).to.equal(1);
            chai_1.expect(data[0].values[0].y).to.equal(2);
            chai_1.expect(data[1].values[0].y).to.equal(3);
            d3Timer.timerFlush(); // this makes sure the animations are all executed
            const heights = bars.d3el
                .selectAll('.bargroup rect.bar')
                .nodes()
                .map((el) => Number(el.getAttribute('height')));
            const height = objects.figure.plotarea_height;
            chai_1.expect(heights).to.deep.equal([(height * 2) / 3, height]);
        });
    });
    it('create 2d classic', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = [0, 0.5, 1];
            const y = [
                [2, 2.0, 3],
                [2.0, 2.5, 3.5],
            ];
            const objects = yield widget_utils_1.create_figure_bars(this.manager, x, y);
            const bars = objects.bars;
            const data = bars.d3el.selectAll('.bargroup').data();
            chai_1.expect(data).to.have.lengthOf(3);
            chai_1.expect(data[0].values[0].x).to.equal(0);
            chai_1.expect(data[0].values[0].y).to.equal(2);
            chai_1.expect(data[0].values[1].x).to.equal(0);
            chai_1.expect(data[0].values[1].y).to.equal(2);
            chai_1.expect(data[0].values).to.have.lengthOf(2);
            chai_1.expect(data[1].values[0].x).to.equal(0.5);
            chai_1.expect(data[1].values[0].y).to.equal(2);
            chai_1.expect(data[1].values[1].x).to.equal(0.5);
            chai_1.expect(data[1].values[1].y).to.equal(2.5);
            chai_1.expect(data[1].values).to.have.lengthOf(2);
            chai_1.expect(data[2].values[0].x).to.equal(1);
            chai_1.expect(data[2].values[0].y).to.equal(3);
            chai_1.expect(data[2].values[1].x).to.equal(1);
            chai_1.expect(data[2].values[1].y).to.equal(3.5);
            chai_1.expect(data[2].values).to.have.lengthOf(2);
            d3Timer.timerFlush(); // this makes sure the animations are all executed
            const heights = bars.d3el
                .selectAll('.bargroup rect.bar')
                .nodes()
                .map((el) => Math.round(Number(el.getAttribute('height'))));
            const height = objects.figure.plotarea_height;
            const h0 = Math.round((height * 2) / 3);
            const h2 = Math.round(height);
            const h3 = Math.round((height * 2.5) / 3);
            const h4 = Math.round((height * 3.5) / 3);
            chai_1.expect(heights).to.deep.equal([h0, h0, h0, h3, h2, h4]);
        });
    });
    it('create 2d binary', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 0.5, 1]).buffer),
                shape: [3],
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 2.0, 3, 2.0, 2.5, 3.5]).buffer),
                shape: [2, 3],
            };
            const objects = yield widget_utils_1.create_figure_bars(this.manager, x, y);
            const bars = objects.bars;
            const data = bars.d3el.selectAll('.bargroup').data();
            chai_1.expect(data).to.have.lengthOf(3);
            chai_1.expect(data[0].values[0].x).to.equal(0);
            chai_1.expect(data[0].values[0].y).to.equal(2);
            chai_1.expect(data[0].values[1].x).to.equal(0);
            chai_1.expect(data[0].values[1].y).to.equal(2);
            chai_1.expect(data[0].values).to.have.lengthOf(2);
            chai_1.expect(data[1].values[0].x).to.equal(0.5);
            chai_1.expect(data[1].values[0].y).to.equal(2);
            chai_1.expect(data[1].values[1].x).to.equal(0.5);
            chai_1.expect(data[1].values[1].y).to.equal(2.5);
            chai_1.expect(data[1].values).to.have.lengthOf(2);
            chai_1.expect(data[2].values[0].x).to.equal(1);
            chai_1.expect(data[2].values[0].y).to.equal(3);
            chai_1.expect(data[2].values[1].x).to.equal(1);
            chai_1.expect(data[2].values[1].y).to.equal(3.5);
            chai_1.expect(data[2].values).to.have.lengthOf(2);
            d3Timer.timerFlush(); // this makes sure the animations are all executed
            const heights = bars.d3el
                .selectAll('.bargroup rect.bar')
                .nodes()
                .map((el) => Math.round(Number(el.getAttribute('height'))));
            const height = objects.figure.plotarea_height;
            const h0 = Math.round((height * 2) / 3);
            const h2 = Math.round(height);
            const h3 = Math.round((height * 2.5) / 3);
            const h4 = Math.round((height * 3.5) / 3);
            chai_1.expect(heights).to.deep.equal([h0, h0, h0, h3, h2, h4]);
        });
    });
    it('selected', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = [0, 1, 2, 3, 5, 6];
            const y = [2, 3, 4, 5, 1, 2];
            const objects = yield widget_utils_1.create_figure_bars(this.manager, x, y);
            const bars = objects.bars;
            const rects = bars.d3el.selectAll('.bargroup').selectAll('.bar');
            bars.model.set('selected_style', { fill: 'orange' });
            bars.model.set('unselected_style', { fill: 'red' });
            // Default color, no selection
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'steelblue',
                'steelblue',
                'steelblue',
                'steelblue',
                'steelblue',
                'steelblue',
            ]);
            // Simulate selection from back-end
            bars.model.set('selected', [0, 3]);
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'orange',
                'red',
                'red',
                'orange',
                'red',
                'red',
            ]);
            bars.model.set('selected', [4, 5]);
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'red',
                'red',
                'red',
                'red',
                'orange',
                'orange',
            ]);
            bars.model.set('selected_style', { fill: 'green' });
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'red',
                'red',
                'red',
                'red',
                'green',
                'green',
            ]);
            bars.model.set('unselected_style', { fill: 'black' });
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'black',
                'black',
                'black',
                'black',
                'green',
                'green',
            ]);
            bars.model.set('selected_style', {});
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'black',
                'black',
                'black',
                'black',
                'steelblue',
                'steelblue',
            ]);
            bars.model.set('selected', null);
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'steelblue',
                'steelblue',
                'steelblue',
                'steelblue',
                'steelblue',
                'steelblue',
            ]);
        });
    });
    it('styles', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = [0, 1, 2, 3, 5, 6];
            const y = [2, 3, 4, 5, 1, 2];
            const objects = yield widget_utils_1.create_figure_bars(this.manager, x, y);
            const bars = objects.bars;
            const rects = bars.d3el.selectAll('.bargroup').selectAll('.bar');
            bars.model.set('fill', false);
            // No fill
            chai_1.expect(widget_utils_1.getFills(rects)).to.eql([
                'none',
                'none',
                'none',
                'none',
                'none',
                'none',
            ]);
            chai_1.expect(widget_utils_1.getStrokes(rects)).to.eql([
                'none',
                'none',
                'none',
                'none',
                'none',
                'none',
            ]);
            chai_1.expect(widget_utils_1.getStrokeWidth(rects)).to.eql(['1', '1', '1', '1', '1', '1']);
            bars.model.set('stroke', 'red');
            chai_1.expect(widget_utils_1.getStrokes(rects)).to.eql([
                'red',
                'red',
                'red',
                'red',
                'red',
                'red',
            ]);
            chai_1.expect(widget_utils_1.getStrokeWidth(rects)).to.eql(['1', '1', '1', '1', '1', '1']);
            bars.model.set('stroke_width', 3);
            chai_1.expect(widget_utils_1.getStrokes(rects)).to.eql([
                'red',
                'red',
                'red',
                'red',
                'red',
                'red',
            ]);
            chai_1.expect(widget_utils_1.getStrokeWidth(rects)).to.eql(['3', '3', '3', '3', '3', '3']);
        });
    });
});
//# sourceMappingURL=bars.js.map