"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.locateByClass = exports.locateByContentDesc = exports.locateByResourceId = void 0;
const AppiumLocatorBuilderBase_1 = __importStar(require("../AppiumLocatorBuilderBase"));
const elementUtil_1 = require("../elementUtil");
exports.locateByResourceId = {
    locate: async (driver, selector) => {
        if (!selector.resourceId) {
            return new Set();
        }
        const candidates = await driver.findElements('id', selector.resourceId);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed() {
        return ['resourceId'];
    },
};
exports.locateByContentDesc = {
    locate: async (driver, selector) => {
        if (!selector.contentDesc) {
            return new Set();
        }
        const candidates = await driver.findElements('accessibility id', selector.contentDesc);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed() {
        return ['contentDesc'];
    },
};
const configureFindKeysForByClassLocator = [
    { key: 'resourceId', appiumLocatorType: 'id' },
    { key: 'contentDesc', appiumLocatorType: 'accessibility id' },
];
function filterByClass(elements, expectedClass) {
    return elements.filter((element) => element['attribute/class'] === expectedClass);
}
exports.locateByClass = {
    locate: async (driver, selector, propertyPreferences) => {
        if (!selector.class) {
            return new Set();
        }
        if (propertyPreferences) {
            for (const { key, appiumLocatorType, } of configureFindKeysForByClassLocator) {
                const comparator = AppiumLocatorBuilderBase_1.default.getClauseComparator(propertyPreferences, key);
                if (comparator === AppiumLocatorBuilderBase_1.AppiumClauseComparator.Equals) {
                    const value = propertyPreferences[key].value;
                    const candidates = await driver.findElements(appiumLocatorType, value);
                    const classMatched = filterByClass(candidates, selector.class);
                    if (classMatched.length > 0) {
                        return (0, elementUtil_1.uniqueElementReferenceSet)(classMatched);
                    }
                    return new Set();
                }
            }
        }
        const candidates = await driver.findElements('class name', selector.class);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed(propertyPreferences) {
        if (propertyPreferences) {
            for (const { key } of configureFindKeysForByClassLocator) {
                if (AppiumLocatorBuilderBase_1.default.getClauseComparator(propertyPreferences, key) === AppiumLocatorBuilderBase_1.AppiumClauseComparator.Equals) {
                    return ['class', key];
                }
            }
        }
        return ['class'];
    },
};
