"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeNewLineCharacters = removeNewLineCharacters;
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
class AttributeMatcher {
    initialWeight;
    selectorKey;
    important;
    weight;
    constructor(initialWeight, selectorKey, important = false) {
        this.initialWeight = initialWeight;
        this.selectorKey = selectorKey;
        this.important = important;
        this.weight = initialWeight;
    }
    getWeight() {
        return this.weight;
    }
    calculateMatchScore(originalSelector, candidateSelector, preferences) {
        return this.canMatchBy(originalSelector, preferences) &&
            this.areMatchedNormalized(originalSelector, candidateSelector, preferences)
            ? this.weight
            : 0;
    }
    canMatchBy(selector, preferences) {
        const expectedValue = this.getEffectiveSelectorValue(this.selectorKey, selector, preferences);
        return expectedValue !== undefined && expectedValue !== '';
    }
    getEffectiveSelectorValue(propertyKey, selector, preferences) {
        const selectorValue = selector[propertyKey]?.toString();
        const preference = preferences?.[propertyKey];
        return preference?.intended && preference.value !== undefined
            ? preference.value
            : selectorValue;
    }
    getMatchedCandidates(selector, findCandidates, preferences) {
        return findCandidates.filter((candidate) => this.calculateMatchScore(selector, candidate.selector, preferences) > 0);
    }
    getMatchedCandidatesCount(selector, findCandidates, preferences) {
        return this.getMatchedCandidates(selector, findCandidates, preferences)
            .length;
    }
    adjustWeightBySpecificity(findCandidates, selector, preferences) {
        this.weight = this.initialWeight;
        const matchCount = this.getMatchedCandidatesCount(selector, findCandidates, preferences);
        this.weight = matchCount > 0 ? this.initialWeight / matchCount : 0.0;
    }
    areMatchedNormalized(originalSelector, candidateSelector, preferences) {
        const expectedValue = this.getEffectiveSelectorValue(this.selectorKey, originalSelector, preferences);
        const candidateSelectorValue = this.getEffectiveSelectorValue(this.selectorKey, candidateSelector, undefined);
        const comparator = preferences?.[this.selectorKey]?.comparator ??
            mablscriptFind_1.PropertyComparator.EQUALS;
        if (typeof expectedValue === 'string' &&
            typeof candidateSelectorValue === 'string') {
            const compareResult = (0, mablscriptFind_1.compareStrings)(expectedValue, candidateSelectorValue, comparator);
            if (compareResult) {
                return true;
            }
            return (0, mablscriptFind_1.compareStrings)(removeNewLineCharacters(expectedValue), removeNewLineCharacters(candidateSelectorValue), comparator);
        }
        return false;
    }
}
exports.default = AttributeMatcher;
function removeNewLineCharacters(val) {
    const regex = /(\n|\r|[^\x00-\x7F])/g;
    return val.replace(regex, ' ');
}
