<commit_before>/**
 * Copyright 2015-2019 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {LitElement, html} from "lit";
import LitUtils from "../utils/lit-utils.js";
import UtilsNew from "../../../core/utilsNew.js";
import "../forms/select-field-filter.js";

export default class ConsequenceTypeSelectFilter extends LitElement {

    constructor() {
        super();

        // Set status and init private properties
        this._init();
    }

    createRenderRoot() {
        return this;
    }

    static get properties() {
        return {
            ct: {
                type: String
            },
            config: {
                type: Object
            }
        };
    }

    _init() {
        this._prefix = UtilsNew.randomString(8);

        this._ct = []; // this.ct is a comma separated list, this._ct is an array of the same data
        this.presetSelected = new Map();
        // this.isChecked = {};
        this.options = [];
    }

    connectedCallback() {
        super.connectedCallback();

        this._config = {...this.getDefaultConfig(), ...this.config};
        this.options = this._config.categories.map(item => item.title ?
            {
                id: item.title.toUpperCase(),
                fields: item.terms.map(term => this.mapTerm(term))
            } :
            this.mapTerm(item)
        );
    }

    // eslint-disable-next-line no-unused-vars
    firstUpdated(changedProperties) {
        // Display SO terms in the badgers
        UtilsNew.initTooltip(this);
    }

    update(changedProperties) {
        if (changedProperties.has("ct")) {
            if (this.ct) {
                this._ct = this.ct.split(",");
                this.presetSelected = new Map(); // Reset active presets

                // Add active presets using selected CT terms
                (this._config.alias || []).forEach(preset => {
                    const allTermsSelected = preset.terms.every(term => this._ct.includes(term));
                    if (allTermsSelected) {
                        this.presetSelected.set(preset.name, preset);
                    }
                });
            } else {
                this._ct = [];
                // this.isChecked = {};
            }
        }
        super.update(changedProperties);
    }

    mapTerm(term) {
        // TODO think about this badge:
        // <span class='badge badge-light' style="color: ${CONSEQUENCE_TYPES.style[term.impact]}">${term.impact}</span>
        return {
            id: term.name,
            name: `${term.name}  <span class="badge badge-light">${term.id}</span>`
        };
    }

    onPresetSelect(preset, e) {
        if (preset && this._config.alias) {<commit_msg>wc: revert auto-checking presets behavior in CT filter (#376)<commit_after>/**
 * Copyright 2015-2019 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {LitElement, html} from "lit";
import LitUtils from "../utils/lit-utils.js";
import UtilsNew from "../../../core/utilsNew.js";
import "../forms/select-field-filter.js";

export default class ConsequenceTypeSelectFilter extends LitElement {

    constructor() {
        super();

        // Set status and init private properties
        this._init();
    }

    createRenderRoot() {
        return this;
    }

    static get properties() {
        return {
            ct: {
                type: String
            },
            config: {
                type: Object
            }
        };
    }

    _init() {
        this._prefix = UtilsNew.randomString(8);

        this._ct = []; // this.ct is a comma separated list, this._ct is an array of the same data
        this.presetSelected = new Map();
        // this.isChecked = {};
        this.options = [];
    }

    connectedCallback() {
        super.connectedCallback();

        this._config = {...this.getDefaultConfig(), ...this.config};
        this.options = this._config.categories.map(item => item.title ?
            {
                id: item.title.toUpperCase(),
                fields: item.terms.map(term => this.mapTerm(term))
            } :
            this.mapTerm(item)
        );
    }

    // eslint-disable-next-line no-unused-vars
    firstUpdated(changedProperties) {
        // Display SO terms in the badgers
        UtilsNew.initTooltip(this);
    }

    update(changedProperties) {
        if (changedProperties.has("ct")) {
            this.consequenceTypesObserver();
        }

        super.update(changedProperties);
    }

    consequenceTypesObserver() {
        if (this.ct) {
            this._ct = this.ct.split(",");

            // Josemi 2021-01-27 NOTE: this implementation has been reverted. We need to discuss this behavior in the future.
            // See issue https://github.com/opencb/jsorolla/issues/376
            // Add active presets using selected CT terms
            // this.presetSelected = new Map(); // Reset active presets
            // (this._config.alias || []).forEach(preset => {
            //     const allTermsSelected = preset.terms.every(term => this._ct.includes(term));
            //     if (allTermsSelected) {
            //         this.presetSelected.set(preset.name, preset);
            //     }
            // });
        } else {
            this._ct = [];
            // this.isChecked = {};
        }
    }

    mapTerm(term) {
        // TODO think about this badge:
        // <span class='badge badge-light' style="color: ${CONSEQUENCE_TYPES.style[term.impact]}">${term.impact}</span>
        return {
            id: term.name,
            name: `${term.name}  <span class="badge badge-light">${term.id}</span>`
        };
    }

    onPresetSelect(preset, e) {
        if (preset && this._config.alias) {
{'fraction_non_alphanumeric': 0.12348596750369276, 'fraction_numerical': 0.005908419497784343, 'mean_word_length': 1.8557570645297343, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '2015557', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1801, 'n_words': 490}