"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LegendModifier = void 0;
var EventHandler_1 = require("../../Core/EventHandler");
var ChartModifierType_1 = require("../../types/ChartModifierType");
var SciChartLegend_1 = require("../Visuals/Legend/SciChartLegend");
var ChartModifierBase2D_1 = require("./ChartModifierBase2D");
/**
 * The LegendModifier provides interactive legend behavior on a 2D {@link SciChartSurface}
 * within SciChart - High Performance {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}
 * @remarks
 *
 * To apply the LegendModifier to a {@link SciChartSurface} and add tooltip behavior,
 * use the following code:
 *
 * ```ts
 * const sciChartSurface: SciChartSurface;
 * sciChartSurface.chartModifiers.add(new LegendModifier());
 * ```
 *
 * ---
 * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-modifier-api/miscellaneous-modifiers/legend-modifier/}
 */
var LegendModifier = /** @class */ (function (_super) {
    __extends(LegendModifier, _super);
    /**
     * Creates an instance of the LegendModifier
     * @param options Optional parameters {@link ILegendModifierOptions} used to configure the modifier
     *
     * ---
     * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-modifier-api/miscellaneous-modifiers/legend-modifier/}
     */
    function LegendModifier(options) {
        var _this = this;
        var _a;
        _this = _super.call(this, options) || this;
        _this._legendOptions = {};
        _this.type = ChartModifierType_1.EChart2DModifierType.Legend;
        /**
         * An event handler raised when a {@link SciChartLegend} row checkbox is checked or unchecked
         */
        _this.isCheckedChanged = new EventHandler_1.EventHandler();
        _this._legendOptions = __assign({}, options);
        _this.legendItemCheckedChanged = _this.legendItemCheckedChanged.bind(_this);
        _this.sciChartLegend = (_a = options === null || options === void 0 ? void 0 : options.legend) !== null && _a !== void 0 ? _a : new SciChartLegend_1.SciChartLegend(options);
        _this.applyLegendOptions(_this.sciChartLegend, options);
        return _this;
    }
    LegendModifier.prototype.applyLegendOptions = function (legend, options) {
        var _a, _b, _c, _d, _e, _f;
        legend.showCheckboxes = (_a = options === null || options === void 0 ? void 0 : options.showCheckboxes) !== null && _a !== void 0 ? _a : legend.showCheckboxes;
        legend.showSeriesMarkers = (_b = options === null || options === void 0 ? void 0 : options.showSeriesMarkers) !== null && _b !== void 0 ? _b : legend.showSeriesMarkers;
        legend.margin = (_c = options === null || options === void 0 ? void 0 : options.margin) !== null && _c !== void 0 ? _c : legend.margin;
        legend.orientation = (_d = options === null || options === void 0 ? void 0 : options.orientation) !== null && _d !== void 0 ? _d : legend.orientation;
        legend.placement = (_e = options === null || options === void 0 ? void 0 : options.placement) !== null && _e !== void 0 ? _e : legend.placement;
        legend.showLegend = (_f = options === null || options === void 0 ? void 0 : options.showLegend) !== null && _f !== void 0 ? _f : legend.showLegend;
        legend.legendItemCheckedChangedCallback = this.legendItemCheckedChanged;
        if (options === null || options === void 0 ? void 0 : options.isCheckedChangedCallback) {
            this.isCheckedChanged.subscribe(function (arg) {
                return options.isCheckedChangedCallback(arg.series, arg.isChecked, legend);
            });
        }
    };
    /** @inheritDoc */
    LegendModifier.prototype.applyTheme = function (themeProvider) {
        var _a;
        _super.prototype.applyTheme.call(this, themeProvider);
        (_a = this.sciChartLegend) === null || _a === void 0 ? void 0 : _a.applyTheme();
    };
    /** @inheritDoc */
    LegendModifier.prototype.onAttachSeries = function (rs) {
        _super.prototype.onAttachSeries.call(this, rs);
        this.sciChartLegend.setRenderableSeriesArray(this.getIncludedRenderableSeries());
    };
    /** @inheritDoc */
    LegendModifier.prototype.onDetachSeries = function (rs) {
        _super.prototype.onDetachSeries.call(this, rs);
        this.sciChartLegend.setRenderableSeriesArray(this.getIncludedRenderableSeries());
    };
    /** @inheritDoc */
    LegendModifier.prototype.onParentSurfaceRendered = function () {
        _super.prototype.onParentSurfaceRendered.call(this);
    };
    /** @inheritDoc */
    LegendModifier.prototype.onAttach = function () {
        var _a;
        _super.prototype.onAttach.call(this);
        if (!this.sciChartLegend) {
            this.sciChartLegend = (_a = this._legendOptions.legend) !== null && _a !== void 0 ? _a : new SciChartLegend_1.SciChartLegend(this._legendOptions);
            this.applyLegendOptions(this.sciChartLegend, this._legendOptions);
        }
        this.sciChartLegend.attachTo(this.parentSurface);
        this.sciChartLegend.setInvalidateParentSurface(this.parentSurface.invalidateElement);
        this.sciChartLegend.setRenderableSeriesArray(this.getIncludedRenderableSeries());
    };
    /** @inheritDoc */
    LegendModifier.prototype.onDetach = function () {
        var _a, _b;
        if (this.sciChartLegend) {
            this._legendOptions.margin = this.sciChartLegend.margin;
            this._legendOptions.orientation = this.sciChartLegend.orientation;
            this._legendOptions.placement = this.sciChartLegend.placement;
            this._legendOptions.showCheckboxes = this.sciChartLegend.showCheckboxes;
            this._legendOptions.showLegend = this.sciChartLegend.showLegend;
            this._legendOptions.showSeriesMarkers = this.sciChartLegend.showSeriesMarkers;
        }
        _super.prototype.onDetach.call(this);
        (_a = this.sciChartLegend) === null || _a === void 0 ? void 0 : _a.setRenderableSeriesArray([]);
        (_b = this.sciChartLegend) === null || _b === void 0 ? void 0 : _b.detach();
        this.sciChartLegend = undefined;
    };
    /** @inheritDoc */
    LegendModifier.prototype.includeSeries = function (series, isIncluded) {
        var _a;
        if (_super.prototype.includeSeries.call(this, series, isIncluded)) {
            if (this.isAttached) {
                this.sciChartLegend.setRenderableSeriesArray(this.getIncludedRenderableSeries());
            }
            (_a = this.parentSurface) === null || _a === void 0 ? void 0 : _a.invalidateElement();
            return true;
        }
        return false;
    };
    /** @inheritDoc */
    LegendModifier.prototype.testIsIncludedSeries = function (series) {
        return this.includedSeries.testIsIncluded(series.id);
    };
    /** @inheritDoc */
    LegendModifier.prototype.toJSON = function () {
        var json = _super.prototype.toJSON.call(this);
        var options = {
            margin: this.sciChartLegend.margin,
            orientation: this.sciChartLegend.orientation,
            placement: this.sciChartLegend.placement,
            showCheckboxes: this.sciChartLegend.showCheckboxes,
            showLegend: this.sciChartLegend.showLegend,
            showSeriesMarkers: this.sciChartLegend.showSeriesMarkers
        };
        Object.assign(json.options, options);
        return json;
    };
    /** @inheritDoc */
    LegendModifier.prototype.delete = function () {
        _super.prototype.delete.call(this);
        this.onDetach();
    };
    /**
     * Callback called from inner {@link SciChartLegend} when a checkbox is checked or unchecked
     * @param series
     * @param isChecked
     * @protected
     */
    LegendModifier.prototype.legendItemCheckedChanged = function (series, isChecked) {
        var _a;
        (_a = this.isCheckedChanged) === null || _a === void 0 ? void 0 : _a.raiseEvent({ series: series, isChecked: isChecked });
    };
    return LegendModifier;
}(ChartModifierBase2D_1.ChartModifierBase2D));
exports.LegendModifier = LegendModifier;
