"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppiumClauseComparator = exports.AppiumLocatorStrategy = void 0;
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const Candidate_1 = require("./Candidate");
var AppiumLocatorStrategy;
(function (AppiumLocatorStrategy) {
    AppiumLocatorStrategy["AndroidUiAutomator"] = "-android uiautomator";
    AppiumLocatorStrategy["iOSPredicateString"] = "-ios predicate string";
})(AppiumLocatorStrategy || (exports.AppiumLocatorStrategy = AppiumLocatorStrategy = {}));
var AppiumClauseComparator;
(function (AppiumClauseComparator) {
    AppiumClauseComparator["Contains"] = "Contains";
    AppiumClauseComparator["Equals"] = "Equals";
})(AppiumClauseComparator || (exports.AppiumClauseComparator = AppiumClauseComparator = {}));
class AppiumLocatorBuilderBase {
    clauses = [];
    static getClauseComparator(propertyPreferences, propertyName) {
        const propertyPreference = propertyPreferences[propertyName];
        if (!propertyPreference) {
            return undefined;
        }
        if (!propertyPreference.intended ||
            propertyPreference.value === undefined ||
            propertyPreference.value === '' ||
            propertyPreference.comparator === undefined) {
            return undefined;
        }
        return propertyPreference.comparator === mablscriptFind_1.PropertyComparator.CONTAINS
            ? AppiumClauseComparator.Contains
            : AppiumClauseComparator.Equals;
    }
    hasAnyClause() {
        return this.clauses.length > 0;
    }
    enabled(value) {
        return this.pushBoolean('enabled', value);
    }
    selected(value) {
        return this.pushBoolean('selected', value);
    }
    index(value) {
        return this.pushNumber('index', value);
    }
    pushString(key, value, comparator = AppiumClauseComparator.Equals) {
        if (typeof value !== 'string') {
            throw new Error('Invalid input. Expected string.');
        }
        this.clauses.push(this.getStringClause(key, value, comparator));
        return this;
    }
    pushBoolean(key, value) {
        if (typeof value !== 'boolean') {
            try {
                const converted = (0, Candidate_1.parseBoolean)(value);
                if (converted === undefined) {
                    throw new Error('Invalid input. Expected boolean and could not convert string value');
                }
                this.clauses.push(this.getBooleanClause(key, converted));
                return this;
            }
            catch (error) {
                throw new Error('Invalid input. Expected boolean and could not convert');
            }
        }
        this.clauses.push(this.getBooleanClause(key, value));
        return this;
    }
    pushNumber(key, value) {
        if (typeof value !== 'number') {
            const errorMessage = `Invalid input: ${value}. Expected number and could not convert.`;
            try {
                if (value === undefined ||
                    value === null ||
                    typeof value === 'boolean' ||
                    (typeof value === 'string' && value.trim() === '')) {
                    throw new Error(errorMessage);
                }
                const converted = Number(value);
                if (Number.isNaN(converted)) {
                    throw new Error(errorMessage);
                }
                this.clauses.push(this.getNumberClause(key, converted));
                return this;
            }
            catch (error) {
                throw new Error(errorMessage + ` Error: ${error}`);
            }
        }
        this.clauses.push(this.getNumberClause(key, value));
        return this;
    }
    sanitizeInput(value) {
        return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
    }
}
exports.default = AppiumLocatorBuilderBase;
