"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAssertionMablscriptText = getAssertionMablscriptText;
exports.performAssertion = performAssertion;
exports.getShouldTruncateText = getShouldTruncateText;
exports.parseAssertionExpectedValue = parseAssertionExpectedValue;
exports.cleanEmailFindDescriptorTree = cleanEmailFindDescriptorTree;
const AssertStepTypes_1 = __importStar(require("./canonical/AssertStepTypes"));
const type_utils_1 = require("../type-utils");
const variable_utils_1 = require("../variable-utils");
const dom_util_1 = require("@mablhq-internal/mablscript-find/dom-util");
const NATURAL_NUMBER_WITH_LEADING_ZERO = RegExp('^0[0-9]+$');
function getAssertionMablscriptText(assertionType) {
    return (0, type_utils_1.isDefined)(assertionType)
        ? AssertStepTypes_1.ASSERTION_TYPES_AS_MAP[assertionType]?.value
        : undefined;
}
function maybeConvertToLowerCase(text, shouldConvert) {
    return shouldConvert && typeof text === 'string'
        ? text.toUpperCase().toLowerCase()
        : text;
}
function performAssertion(assertionType, actualValue, expectedValue, caseInsensitive = false) {
    let asserted = true;
    actualValue = maybeConvertToLowerCase(actualValue, caseInsensitive);
    expectedValue = maybeConvertToLowerCase(expectedValue, caseInsensitive);
    switch (assertionType) {
        case AssertStepTypes_1.default.EQUALS: {
            const [leftOperand, rightOperand] = valuesForComparison(actualValue, expectedValue);
            asserted = leftOperand === rightOperand;
            break;
        }
        case AssertStepTypes_1.default.DOES_NOT_EQUAL: {
            const [leftOperand, rightOperand] = valuesForComparison(actualValue, expectedValue);
            asserted = leftOperand !== rightOperand;
            break;
        }
        case AssertStepTypes_1.default.CONTAINS:
            asserted = actualValue.indexOf(expectedValue) !== -1;
            break;
        case AssertStepTypes_1.default.DOES_NOT_CONTAIN:
            asserted = actualValue.indexOf(expectedValue) === -1;
            break;
        case AssertStepTypes_1.default.STARTS_WITH:
            asserted = actualValue.startsWith(expectedValue);
            break;
        case AssertStepTypes_1.default.STARTS_WITHOUT:
            asserted = !actualValue.startsWith(expectedValue);
            break;
        case AssertStepTypes_1.default.ENDS_WITH:
            asserted = actualValue.endsWith(expectedValue);
            break;
        case AssertStepTypes_1.default.ENDS_WITHOUT:
            asserted = !actualValue.endsWith(expectedValue);
            break;
        case AssertStepTypes_1.default.GREATER_THAN: {
            const [leftOperand, rightOperand] = valuesForComparison(actualValue, expectedValue);
            asserted = leftOperand > rightOperand;
            break;
        }
        case AssertStepTypes_1.default.LESS_THAN: {
            const [leftOperand, rightOperand] = valuesForComparison(actualValue, expectedValue);
            asserted = leftOperand < rightOperand;
            break;
        }
        case AssertStepTypes_1.default.GREATER_THAN_OR_EQUALS: {
            const [leftOperand, rightOperand] = valuesForComparison(actualValue, expectedValue);
            asserted = leftOperand >= rightOperand;
            break;
        }
        case AssertStepTypes_1.default.LESS_THAN_OR_EQUALS: {
            const [leftOperand, rightOperand] = valuesForComparison(actualValue, expectedValue);
            asserted = leftOperand <= rightOperand;
            break;
        }
        default:
            asserted = false;
            break;
    }
    return asserted;
}
function getShouldTruncateText(assertionType) {
    switch (assertionType) {
        case AssertStepTypes_1.default.CONTAINS:
        case AssertStepTypes_1.default.DOES_NOT_CONTAIN:
        case AssertStepTypes_1.default.STARTS_WITH:
        case AssertStepTypes_1.default.STARTS_WITHOUT:
        case AssertStepTypes_1.default.ENDS_WITH:
        case AssertStepTypes_1.default.ENDS_WITHOUT:
            return false;
        case AssertStepTypes_1.default.EQUALS:
        case AssertStepTypes_1.default.DOES_NOT_EQUAL:
        case AssertStepTypes_1.default.GREATER_THAN:
        case AssertStepTypes_1.default.GREATER_THAN_OR_EQUALS:
        case AssertStepTypes_1.default.LESS_THAN:
        case AssertStepTypes_1.default.LESS_THAN_OR_EQUALS:
        default:
            return true;
    }
}
function valuesForComparison(actualText, expectedValue) {
    const expectedNumeric = Number(expectedValue);
    const actualNumeric = Number(actualText);
    if (!Number.isNaN(expectedNumeric) &&
        !Number.isNaN(actualNumeric) &&
        !NATURAL_NUMBER_WITH_LEADING_ZERO.test(expectedValue)) {
        return [actualNumeric, expectedNumeric];
    }
    const compareResult = actualText < expectedValue ? -1 : actualText === expectedValue ? 0 : 1;
    return [compareResult, 0];
}
function parseAssertionExpectedValue(expectedValue) {
    if (expectedValue === undefined || expectedValue === null) {
        return '';
    }
    if ((0, type_utils_1.isString)(expectedValue)) {
        return (0, dom_util_1.normalizeText)(expectedValue);
    }
    if ((0, variable_utils_1.isVariableOnImport)(expectedValue)) {
        return `{{@${expectedValue.name}}}`;
    }
    return (0, dom_util_1.normalizeText)(JSON.stringify(expectedValue));
}
function cleanEmailFindDescriptorTree(findDescriptor) {
    const cleanedFindDescriptor = { ...findDescriptor };
    if (!cleanedFindDescriptor.emailConfiguration?.emailPreferences ||
        !Object.keys(cleanedFindDescriptor.emailConfiguration?.emailPreferences)
            .length) {
        delete cleanedFindDescriptor.emailConfiguration?.emailPreferences;
    }
    if (!cleanedFindDescriptor.emailConfiguration?.externalEmailPreferences ||
        !Object.keys(cleanedFindDescriptor.emailConfiguration?.externalEmailPreferences).length) {
        delete cleanedFindDescriptor.emailConfiguration?.externalEmailPreferences;
    }
    if (!cleanedFindDescriptor.emailConfiguration ||
        !Object.keys(cleanedFindDescriptor.emailConfiguration).length) {
        delete cleanedFindDescriptor.emailConfiguration;
    }
    return cleanedFindDescriptor;
}
