<commit_before>// Copyright (c) Microsoft Corporation.  All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
import _Global = require("./Core/_Global");
import _WinRT = require("./Core/_WinRT")

import _Base = require("./Core/_Base");
import _BaseUtils = require("./Core/_BaseUtils");

var Constants = {
    accentStyleId: "WinJSAccentsStyle",
    themeDetectionTag: "winjs",

    hoverSelector: "html.win-hoverable",
    lightThemeSelector: ".win-ui-light",
    darkThemeSelector: ".win-ui-dark"
};

var CSSSelectorTokens = [".", "#", ":"];

var UISettings: _WinRT.Windows.UI.ViewManagement.UISettings = null;
var colors: string[] = [];
var isDarkTheme = false;
var rules: { selector: string; props: { name: string; value: ColorTypes; }[]; }[] = [];
var writeRulesTOHandle = -1;

// Enum values align with the colors array indices
export enum ColorTypes {
    accent = 0,
    listSelectRest = 1,
    listSelectHover = 2,
    listSelectPress = 3,
    _listSelectRestInverse = 4,
    _listSelectHoverInverse = 5,
    _listSelectPressInverse = 6,
}

export function createAccentRule(selector: string, props: { name: string; value: ColorTypes; }[]) {
    rules.push({ selector: selector, props: props });
    scheduleWriteRules();
}

function scheduleWriteRules() {
    if (rules.length === 0 || writeRulesTOHandle !== -1) {
        return;
    }
    writeRulesTOHandle = _BaseUtils._setImmediate(() => {
        writeRulesTOHandle = -1;
        cleanup();

        var inverseThemeSelector = isDarkTheme ? Constants.lightThemeSelector : Constants.darkThemeSelector;
        var inverseThemeHoverSelector = Constants.hoverSelector + " " + inverseThemeSelector;

        var style = _Global.document.createElement("style");
        style.id = Constants.accentStyleId;
        style.textContent = rules.map(rule => {
            // example rule: { selector: "  .foo,   html.win-hoverable   .bar:hover ,  div:hover  ", props: [{ name: "color", value: 0 }, { name: "background-color", value: 1 } }

            var body = "  " + rule.props.map(prop => prop.name + ": " + colors[prop.value] + ";").join("\n  ");
            // body = color: *accent*; background-color: *listSelectHover*

            var selectorSplit = rule.selector.split(",").map(str => sanitizeSpaces(str)); // [".foo", ".bar:hover", "div"]
            var selector = selectorSplit.join(",\n"); // ".foo, html.win-hoverable .bar:hover, div:hover"
            var css = selector + " {\n" + body + "\n}";
            // css = .foo, html.win-hoverable .bar:hover, div:hover { *body* }

            // Inverse Theme Selectors
            var isThemedColor = rule.props.some(prop => prop.value !== ColorTypes.accent)
            if (isThemedColor) {
                var inverseBody = "  " + rule.props.map(prop => prop.name + ": " + colors[(prop.value ? (prop.value + 3) : prop.value)] + ";").join("\n  ");
                // inverseBody = "color: *accent*; background-color: *listSelectHoverInverse"

                selectorSplit.forEach(sel => {
                    sel = sanitizeSpaces(sel);

                    if (sel.indexOf(Constants.hoverSelector) !== -1 && sel.indexOf(inverseThemeHoverSelector) === -1) {
                        css += ",\n" + sel.replace(Constants.hoverSelector, inverseThemeHoverSelector);
                        var selWithoutHover = sel.replace(Constants.hoverSelector, "").trim();
                        if (CSSSelectorTokens.indexOf(selWithoutHover[0]) !== -1) {
                            css += ",\n" + sel.replace(Constants.hoverSelector + " ", inverseThemeHoverSelector);
                        }
                    } else {
                        css += "\n" + inverseThemeSelector + " " + sel;
                        if (CSSSelectorTokens.indexOf(sel[0]) !== -1) {
                            css += ",\n" + inverseThemeSelector + sel;
                        }
                    }
                    css += " {\n" + inverseBody + "\n}";
                });<commit_msg>Fixed a trailing comma issue in style generation<commit_after>// Copyright (c) Microsoft Corporation.  All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
import _Global = require("./Core/_Global");
import _WinRT = require("./Core/_WinRT")

import _Base = require("./Core/_Base");
import _BaseUtils = require("./Core/_BaseUtils");

var Constants = {
    accentStyleId: "WinJSAccentsStyle",
    themeDetectionTag: "winjs",

    hoverSelector: "html.win-hoverable",
    lightThemeSelector: ".win-ui-light",
    darkThemeSelector: ".win-ui-dark"
};

var CSSSelectorTokens = [".", "#", ":"];

var UISettings: _WinRT.Windows.UI.ViewManagement.UISettings = null;
var colors: string[] = [];
var isDarkTheme = false;
var rules: { selector: string; props: { name: string; value: ColorTypes; }[]; }[] = [];
var writeRulesTOHandle = -1;

// Enum values align with the colors array indices
export enum ColorTypes {
    accent = 0,
    listSelectRest = 1,
    listSelectHover = 2,
    listSelectPress = 3,
    _listSelectRestInverse = 4,
    _listSelectHoverInverse = 5,
    _listSelectPressInverse = 6,
}

export function createAccentRule(selector: string, props: { name: string; value: ColorTypes; }[]) {
    rules.push({ selector: selector, props: props });
    scheduleWriteRules();
}

function scheduleWriteRules() {
    if (rules.length === 0 || writeRulesTOHandle !== -1) {
        return;
    }
    writeRulesTOHandle = _BaseUtils._setImmediate(() => {
        writeRulesTOHandle = -1;
        cleanup();

        var inverseThemeSelector = isDarkTheme ? Constants.lightThemeSelector : Constants.darkThemeSelector;
        var inverseThemeHoverSelector = Constants.hoverSelector + " " + inverseThemeSelector;

        var style = _Global.document.createElement("style");
        style.id = Constants.accentStyleId;
        style.textContent = rules.map(rule => {
            // example rule: { selector: "  .foo,   html.win-hoverable   .bar:hover ,  div:hover  ", props: [{ name: "color", value: 0 }, { name: "background-color", value: 1 } }

            var body = "  " + rule.props.map(prop => prop.name + ": " + colors[prop.value] + ";").join("\n  ");
            // body = color: *accent*; background-color: *listSelectHover*

            var selectorSplit = rule.selector.split(",").map(str => sanitizeSpaces(str)); // [".foo", ".bar:hover", "div"]
            var selector = selectorSplit.join(",\n"); // ".foo, html.win-hoverable .bar:hover, div:hover"
            var css = selector + " {\n" + body + "\n}";
            // css = .foo, html.win-hoverable .bar:hover, div:hover { *body* }

            // Inverse Theme Selectors
            var isThemedColor = rule.props.some(prop => prop.value !== ColorTypes.accent)
            if (isThemedColor) {
                var inverseBody = "  " + rule.props.map(prop => prop.name + ": " + colors[(prop.value ? (prop.value + 3) : prop.value)] + ";").join("\n  ");
                // inverseBody = "color: *accent*; background-color: *listSelectHoverInverse"

                var themedSelectors: string[] = [];
                selectorSplit.forEach(sel => {
                    sel = sanitizeSpaces(sel);

                    if (sel.indexOf(Constants.hoverSelector) !== -1 && sel.indexOf(inverseThemeHoverSelector) === -1) {
                        themedSelectors.push(sel.replace(Constants.hoverSelector, inverseThemeHoverSelector));
                        var selWithoutHover = sel.replace(Constants.hoverSelector, "").trim();
                        if (CSSSelectorTokens.indexOf(selWithoutHover[0]) !== -1) {
                            themedSelectors.push(sel.replace(Constants.hoverSelector + " ", inverseThemeHoverSelector));
                        }
                    } else {
                        themedSelectors.push(inverseThemeSelector + " " + sel);
                        if (CSSSelectorTokens.indexOf(sel[0]) !== -1) {
                            themedSelectors.push(inverseThemeSelector + sel);
                        }
                    }
                    css += "\n" + themedSelectors.join(",\n") + " {\n" + inverseBody + "\n}";
                });
{'fraction_non_alphanumeric': 0.14655486693401384, 'fraction_numerical': 0.004860857941426662, 'mean_word_length': 2.3868312757201644, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2939793', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2306, 'n_words': 514}