/**
 * @dyna-grid/core - Advanced Data Grid / Data Table supporting Javascript / Web Components
 * @version v1.1.0
 * @link 
 * @license 
 */
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Bean, Autowired } from './context/context';
import { BeanStub } from "./context/beanStub";
import { addCssClass } from './utils/dom';
import { doOnce } from './utils/function';
var BASE_GRID_SIZE = 4;
var DEFALUT_GRID_SIZE = 6;
var HARD_CODED_SIZES = {
    // this item is required for custom themes
    'dy-theme-custom': {
        headerHeight: 25,
        headerCellMinWidth: 24,
        listItemHeight: BASE_GRID_SIZE * 5,
        rowHeight: 25,
        chartMenuPanelWidth: 220
    },
    'dy-theme-default': {
        headerHeight: DEFALUT_GRID_SIZE * 8,
        headerCellMinWidth: 36,
        listItemHeight: DEFALUT_GRID_SIZE * 4,
        rowHeight: DEFALUT_GRID_SIZE * 7,
        chartMenuPanelWidth: 240
    }
};
var SASS_PROPERTY_BUILDER = {
    headerHeight: ['dy-header-row'],
    headerCellMinWidth: ['dy-header-cell'],
    listItemHeight: ['dy-virtual-list-item'],
    rowHeight: ['dy-row'],
    chartMenuPanelWidth: ['dy-chart-docked-container']
};
var CALCULATED_SIZES = {};
var Environment = /** @class */ (function (_super) {
    __extends(Environment, _super);
    function Environment() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Environment.prototype.getSassVariable = function (theme, key) {
        var useTheme = 'dy-theme-' + (theme.match('default') ? 'default' : 'custom');
        var defaultValue = HARD_CODED_SIZES[useTheme][key];
        var calculatedValue = 0;
        if (!CALCULATED_SIZES[theme]) {
            CALCULATED_SIZES[theme] = {};
        }
        var size = CALCULATED_SIZES[theme][key];
        if (size != null) {
            return size;
        }
        if (SASS_PROPERTY_BUILDER[key]) {
            var classList = SASS_PROPERTY_BUILDER[key];
            var div = document.createElement('div');
            addCssClass(div, theme);
            div.style.position = 'absolute';
            var el = classList.reduce(function (prevEl, currentClass) {
                var currentDiv = document.createElement('div');
                currentDiv.style.position = 'static';
                addCssClass(currentDiv, currentClass);
                prevEl.appendChild(currentDiv);
                return currentDiv;
            }, div);
            if (document.body) {
                document.body.appendChild(div);
                var sizeName = key.toLowerCase().indexOf('height') !== -1 ? 'height' : 'width';
                calculatedValue = parseInt(window.getComputedStyle(el)[sizeName], 10);
                document.body.removeChild(div);
            }
        }
        CALCULATED_SIZES[theme][key] = calculatedValue || defaultValue;
        return CALCULATED_SIZES[theme][key];
    };
    Environment.prototype.isThemeDark = function () {
        var theme = this.getTheme().theme;
        return !!theme && theme.indexOf('dark') >= 0;
    };
    Environment.prototype.chartMenuPanelWidth = function () {
        var theme = this.getTheme().themeFamily;
        return this.getSassVariable(theme, 'chartMenuPanelWidth');
    };
    Environment.prototype.getTheme = function () {
        var reg = /\bdy-(material|(?:theme-([\w\-]*)))\b/;
        var el = this.eGridDiv;
        var themeMatch = null;
        while (el) {
            themeMatch = reg.exec(el.className);
            if (!themeMatch) {
                el = el.parentElement || undefined;
            }
            else {
                break;
            }
        }
        if (!themeMatch) {
            return {};
        }
        var theme = themeMatch[0];
        var usingOldTheme = themeMatch[2] === undefined;
        if (usingOldTheme) {
            var newTheme_1 = theme.replace('dy-', 'dy-theme-');
            doOnce(function () { return console.warn("DYNA Grid: As of v19 old theme are no longer provided. Please replace " + theme + " with " + newTheme_1 + "."); }, 'using-old-theme');
        }
        return { theme: theme, el: el, themeFamily: theme.replace(/-dark$/, '') };
    };
    __decorate([
        Autowired('eGridDiv')
    ], Environment.prototype, "eGridDiv", void 0);
    Environment = __decorate([
        Bean('environment')
    ], Environment);
    return Environment;
}(BeanStub));
export { Environment };
