"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.locateByType = exports.locateByLabel = exports.locateByAccessibilityId = exports.locateByFullSelector = void 0;
const AppiumLocatorBuilderBase_1 = __importDefault(require("../AppiumLocatorBuilderBase"));
const elementUtil_1 = require("../elementUtil");
const iOSPredicateBuilder_1 = __importDefault(require("./iOSPredicateBuilder"));
exports.locateByFullSelector = {
    locate: async (driver, selector) => {
        const builder = new iOSPredicateBuilder_1.default();
        if (selector.name !== undefined) {
            builder.name(selector.name);
        }
        if (selector.label !== undefined) {
            builder.label(selector.label);
        }
        if (selector.type !== undefined) {
            builder.type(selector.type);
        }
        if (selector.visible !== undefined) {
            builder.visible(selector.visible);
        }
        if (selector.enabled !== undefined) {
            builder.enabled(selector.enabled);
        }
        if (selector.selected !== undefined) {
            builder.selected(selector.selected);
        }
        if (!builder.hasAnyClause()) {
            return new Set();
        }
        const { strategy, appiumSelector } = builder.toLocator();
        const candidates = await driver.findElements(strategy, appiumSelector);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed() {
        return ['name', 'label', 'type', 'visible', 'enabled', 'selected'];
    },
};
exports.locateByAccessibilityId = {
    locate: async (driver, selector) => {
        if (!selector.name) {
            return new Set();
        }
        const candidates = await driver.findElements('name', selector.name);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed() {
        return ['name'];
    },
};
exports.locateByLabel = {
    locate: async (driver, selector) => {
        if (!selector.label) {
            return new Set();
        }
        const builder = new iOSPredicateBuilder_1.default().label(selector.label);
        const { strategy, appiumSelector } = builder.toLocator();
        const candidates = await driver.findElements(strategy, appiumSelector);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed() {
        return ['label'];
    },
};
const configureFindKeysForByTypeLocator = [
    'label',
    'name',
    'value',
    'index',
    'enabled',
    'selected',
];
exports.locateByType = {
    locate: async (driver, selector, propertyPreferences) => {
        if (!selector.type) {
            return new Set();
        }
        const builder = new iOSPredicateBuilder_1.default().type(selector.type);
        if (propertyPreferences) {
            for (const propertyName of configureFindKeysForByTypeLocator) {
                const comparator = AppiumLocatorBuilderBase_1.default.getClauseComparator(propertyPreferences, propertyName);
                if (comparator) {
                    switch (propertyName) {
                        case 'label':
                            builder.label(propertyPreferences[propertyName].value, comparator);
                            break;
                        case 'name':
                            builder.name(propertyPreferences[propertyName].value, comparator);
                            break;
                        case 'value':
                            builder.value(propertyPreferences[propertyName].value, comparator);
                            break;
                        case 'index':
                            builder.index(parseInt(propertyPreferences[propertyName].value, 10));
                            break;
                        case 'enabled':
                            builder.enabled(propertyPreferences[propertyName].value === 'true');
                            break;
                        case 'selected':
                            builder.selected(propertyPreferences[propertyName].value === 'true');
                            break;
                        default:
                    }
                }
            }
        }
        const { strategy, appiumSelector } = builder.toLocator();
        const candidates = await driver.findElements(strategy, appiumSelector);
        return (0, elementUtil_1.uniqueElementReferenceSet)(candidates);
    },
    getPropertiesUsed(propertyPreferences) {
        const propertiesUsed = ['type'];
        if (propertyPreferences) {
            for (const propertyName of configureFindKeysForByTypeLocator) {
                if (AppiumLocatorBuilderBase_1.default.getClauseComparator(propertyPreferences, propertyName)) {
                    propertiesUsed.push(propertyName);
                }
            }
        }
        return propertiesUsed;
    },
};
