/**
 * DevExtreme (esm/viz/components/chart_theme_manager.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    noop
} from "../../core/utils/common";
import {
    isString as _isString,
    isDefined as _isDefined,
    isNumeric,
    isPlainObject
} from "../../core/utils/type";
import {
    extend
} from "../../core/utils/extend";
import {
    BaseThemeManager
} from "../core/base_theme_manager";
import {
    normalizeEnum as _normalizeEnum,
    extractColor
} from "../core/utils";
export const ThemeManager = BaseThemeManager.inherit(function() {
    const processAxisOptions = function(axisOptions) {
        if (!axisOptions) {
            return {}
        }
        axisOptions = extend(true, {}, axisOptions);
        axisOptions.title = (options = axisOptions.title, _isString(options) ? {
            text: options
        } : options);
        var options;
        if ("logarithmic" === axisOptions.type && axisOptions.logarithmBase <= 0 || axisOptions.logarithmBase && !isNumeric(axisOptions.logarithmBase)) {
            axisOptions.logarithmBase = void 0;
            axisOptions.logarithmBaseError = true
        }
        if (axisOptions.label) {
            if (axisOptions.label.alignment) {
                axisOptions.label.userAlignment = true
            }
        }
        return axisOptions
    };
    const applyParticularAxisOptions = function(name, userOptions, rotated) {
        const theme = this._theme;
        const position = !(rotated ^ "valueAxis" === name) ? "horizontalAxis" : "verticalAxis";
        const processedUserOptions = processAxisOptions(userOptions);
        const commonAxisSettings = processAxisOptions(this._userOptions.commonAxisSettings);
        const mergeOptions = extend(true, {}, theme.commonAxisSettings, theme[position], theme[name], commonAxisSettings, processedUserOptions);
        mergeOptions.workWeek = processedUserOptions.workWeek || theme[name].workWeek;
        mergeOptions.forceUserTickInterval |= _isDefined(processedUserOptions.tickInterval) && !_isDefined(processedUserOptions.axisDivisionFactor);
        return mergeOptions
    };
    const mergeOptions = function(name, userOptions) {
        userOptions = userOptions || this._userOptions[name];
        const theme = this._theme[name];
        let result = this._mergedSettings[name];
        if (result) {
            return result
        }
        if (isPlainObject(theme) && isPlainObject(userOptions)) {
            result = extend(true, {}, theme, userOptions)
        } else {
            result = _isDefined(userOptions) ? userOptions : theme
        }
        this._mergedSettings[name] = result;
        return result
    };
    const applyParticularTheme = {
        base: mergeOptions,
        argumentAxis: applyParticularAxisOptions,
        valueAxisRangeSelector: function() {
            return mergeOptions.call(this, "valueAxis")
        },
        valueAxis: applyParticularAxisOptions,
        series: function(name, userOptions, seriesCount) {
            const that = this;
            const theme = that._theme;
            let userCommonSettings = that._userOptions.commonSeriesSettings || {};
            const themeCommonSettings = theme.commonSeriesSettings;
            const widgetType = that._themeSection.split(".").slice(-1)[0];
            const type = _normalizeEnum(userOptions.type || userCommonSettings.type || themeCommonSettings.type || "pie" === widgetType && theme.type);
            const palette = that.palette;
            const isBar = ~type.indexOf("bar");
            const isLine = ~type.indexOf("line");
            const isArea = ~type.indexOf("area");
            const isBubble = "bubble" === type;
            let mainSeriesColor;
            const resolveLabelsOverlapping = that.getOptions("resolveLabelsOverlapping");
            const containerBackgroundColor = that.getOptions("containerBackgroundColor");
            const seriesTemplate = applyParticularTheme.seriesTemplate.call(this);
            let seriesVisibility;
            if (isBar || isBubble) {
                userOptions = extend(true, {}, userCommonSettings, userCommonSettings[type], userOptions);
                seriesVisibility = userOptions.visible;
                userCommonSettings = {
                    type: {}
                };
                extend(true, userOptions, userOptions.point);
                userOptions.visible = seriesVisibility
            }
            const settings = extend(true, {
                aggregation: {}
            }, themeCommonSettings, themeCommonSettings[type], userCommonSettings, userCommonSettings[type], userOptions);
            settings.aggregation.enabled = "chart" === widgetType && !!settings.aggregation.enabled;
            settings.type = type;
            settings.widgetType = widgetType;
            settings.containerBackgroundColor = containerBackgroundColor;
            if ("pie" !== widgetType) {
                mainSeriesColor = extractColor(settings.color, true) || palette.getNextColor(seriesCount)
            } else {
                mainSeriesColor = function(argument, index, count) {
                    const cat = `${argument}-${index}`;
                    if (!that._multiPieColors[cat]) {
                        that._multiPieColors[cat] = palette.getNextColor(count)
                    }
                    return that._multiPieColors[cat]
                }
            }
            settings.mainSeriesColor = mainSeriesColor;
            settings.resolveLabelsOverlapping = resolveLabelsOverlapping;
            if (settings.label && (isLine || isArea && "rangearea" !== type || "scatter" === type)) {
                settings.label.position = "outside"
            }
            if (seriesTemplate) {
                settings.nameField = seriesTemplate.nameField
            }
            return settings
        },
        animation: function(name) {
            let userOptions = this._userOptions[name];
            userOptions = isPlainObject(userOptions) ? userOptions : _isDefined(userOptions) ? {
                enabled: !!userOptions
            } : {};
            return mergeOptions.call(this, name, userOptions)
        },
        seriesTemplate() {
            const value = mergeOptions.call(this, "seriesTemplate");
            if (value) {
                value.nameField = value.nameField || "series"
            }
            return value
        },
        zoomAndPan() {
            function parseOption(option) {
                option = _normalizeEnum(option);
                const pan = "pan" === option || "both" === option;
                const zoom = "zoom" === option || "both" === option;
                return {
                    pan: pan,
                    zoom: zoom,
                    none: !pan && !zoom
                }
            }
            const options = mergeOptions.call(this, "zoomAndPan");
            return {
                valueAxis: parseOption(options.valueAxis),
                argumentAxis: parseOption(options.argumentAxis),
                dragToZoom: !!options.dragToZoom,
                dragBoxStyle: {
                    class: "dxc-shutter",
                    fill: options.dragBoxStyle.color,
                    opacity: options.dragBoxStyle.opacity
                },
                panKey: options.panKey,
                allowMouseWheel: !!options.allowMouseWheel,
                allowTouchGestures: !!options.allowTouchGestures
            }
        }
    };
    return {
        _themeSection: "chart",
        ctor: function(params) {
            this.callBase.apply(this, arguments);
            const options = params.options || {};
            this._userOptions = options;
            this._mergeAxisTitleOptions = [];
            this._multiPieColors = {};
            this._callback = noop
        },
        dispose: function() {
            this.palette && this.palette.dispose();
            this.palette = this._userOptions = this._mergedSettings = this._multiPieColors = null;
            return this.callBase.apply(this, arguments)
        },
        resetPalette: function() {
            this.palette.reset();
            this._multiPieColors = {}
        },
        getOptions: function(name) {
            return (applyParticularTheme[name] || applyParticularTheme.base).apply(this, arguments)
        },
        refresh: function() {
            this._mergedSettings = {};
            return this.callBase.apply(this, arguments)
        },
        _initializeTheme: function() {
            this.callBase.apply(this, arguments);
            this.updatePalette()
        },
        resetOptions: function(name) {
            this._mergedSettings[name] = null
        },
        update: function(options) {
            this._userOptions = options
        },
        updatePalette: function() {
            this.palette = this.createPalette(this.getOptions("palette"), {
                useHighlight: true,
                extensionMode: this.getOptions("paletteExtensionMode")
            })
        }
    }
}());
