"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.atIndex = atIndex;
exports.locatorByLabelOrText = locatorByLabelOrText;
exports.fieldByName = fieldByName;
exports.locatorByAny = locatorByAny;
exports.buttonByName = buttonByName;
exports.resolveSelectorAlias = resolveSelectorAlias;
function atIndex(locator, index) {
    return index === 0 ? locator.first() : locator.nth(index);
}
function locatorByLabelOrText(page, name, selectors) {
    const selector = resolveSelectorAlias(name, selectors);
    const base = page.getByLabel(name).or(page.getByText(name, { exact: true }));
    return selector ? page.locator(selector).or(base) : base;
}
function fieldByName(page, name, selectors) {
    const selector = resolveSelectorAlias(name, selectors);
    if (selector) {
        return page.locator(selector);
    }
    return page.getByLabel(name);
}
function locatorByAny(page, target, selectors) {
    const escapedText = escapeText(target);
    const escapedCss = cssEscape(target);
    const escapedAttribute = escapeAttribute(target);
    const escapedRegex = escapeRegExp(target);
    const plainTarget = target.trim().replace(/^[#.]/, '') || '__never_match__';
    const plainEscapedCss = cssEscape(plainTarget);
    const plainEscapedAttribute = escapeAttribute(plainTarget);
    const selector = resolveSelectorAlias(target, selectors);
    const base = page
        .getByRole('button', { name: target, exact: true })
        .or(page.getByRole('button', { name: new RegExp(escapedRegex, 'i') }))
        .or(page.getByRole('link', { name: target, exact: true }))
        .or(page.getByRole('link', { name: new RegExp(escapedRegex, 'i') }))
        .or(page.getByLabel(target))
        .or(page.getByPlaceholder(target))
        .or(page.getByText(target, { exact: true }))
        .or(page.locator(`label-cr:has-text("${escapedText}")`))
        .or(page.locator(`label:has-text("${escapedText}")`))
        .or(page.locator(`[data-test-id="${escapedAttribute}"]`))
        .or(page.locator(`[data-testid="${escapedAttribute}"]`))
        .or(page.locator(`[name="${escapedAttribute}"]`))
        .or(page.locator(`[aria-label="${escapedAttribute}"]`))
        .or(page.locator(`[col-id="${escapedAttribute}"]`))
        .or(page.locator(`[row-id="${escapedAttribute}"]`))
        .or(page.locator(`#${escapedCss}`))
        .or(page.locator(`.${escapedCss}`))
        .or(page.locator(`#${plainEscapedCss}`))
        .or(page.locator(`.${plainEscapedCss}`))
        .or(page.locator(`[id="${plainEscapedAttribute}"]`))
        .or(page.locator(`[class~="${plainEscapedAttribute}"]`))
        .or(page.locator(`[data-test-id="${plainEscapedAttribute}"]`))
        .or(page.locator(`[data-testid="${plainEscapedAttribute}"]`))
        .or(page.locator(target));
    return selector ? page.locator(selector).or(base) : base;
}
function buttonByName(page, name, selectors) {
    const base = page
        .getByRole('button', { name, exact: true })
        .or(page.getByRole('button', { name: new RegExp(escapeRegExp(name), 'i') }));
    const selector = resolveSelectorAlias(name, selectors);
    return selector ? page.locator(selector).or(base) : base;
}
function resolveSelectorAlias(target, selectors) {
    return selectors?.[target];
}
function escapeRegExp(value) {
    return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function escapeText(value) {
    return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
function escapeAttribute(value) {
    return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
function cssEscape(value) {
    return value.replace(/([!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~])/g, '\\$1').replace(/\s/g, '\\ ');
}
