/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { asLocator } from '@isomorphic/locatorGenerators';
import { getByAltTextSelector, getByLabelSelector, getByPlaceholderSelector, getByRoleSelector, getByTestIdSelector, getByTextSelector, getByTitleSelector } from '@isomorphic/locatorUtils';
import { escapeForTextSelector } from '@isomorphic/stringUtils';
const selectorSymbol = Symbol('selector');
class Locator {
    [selectorSymbol];
    element;
    elements;
    constructor(injectedScript, selector, options) {
        if (options?.hasText)
            selector += ` >> internal:has-text=${escapeForTextSelector(options.hasText, false)}`;
        if (options?.hasNotText)
            selector += ` >> internal:has-not-text=${escapeForTextSelector(options.hasNotText, false)}`;
        if (options?.has)
            selector += ` >> internal:has=` + JSON.stringify(options.has[selectorSymbol]);
        if (options?.hasNot)
            selector += ` >> internal:has-not=` + JSON.stringify(options.hasNot[selectorSymbol]);
        if (options?.visible !== undefined)
            selector += ` >> visible=${options.visible ? 'true' : 'false'}`;
        this[selectorSymbol] = selector;
        if (selector) {
            const parsed = injectedScript.parseSelector(selector);
            this.element = injectedScript.querySelector(parsed, injectedScript.document, false);
            this.elements = injectedScript.querySelectorAll(parsed, injectedScript.document);
        }
        const selectorBase = selector;
        const self = this;
        self.locator = (selector, options) => {
            return new Locator(injectedScript, selectorBase ? selectorBase + ' >> ' + selector : selector, options);
        };
        self.getByTestId = (testId) => self.locator(getByTestIdSelector(injectedScript.testIdAttributeNameForStrictErrorAndConsoleCodegen(), testId));
        self.getByAltText = (text, options) => self.locator(getByAltTextSelector(text, options));
        self.getByLabel = (text, options) => self.locator(getByLabelSelector(text, options));
        self.getByPlaceholder = (text, options) => self.locator(getByPlaceholderSelector(text, options));
        self.getByText = (text, options) => self.locator(getByTextSelector(text, options));
        self.getByTitle = (text, options) => self.locator(getByTitleSelector(text, options));
        self.getByRole = (role, options = {}) => self.locator(getByRoleSelector(role, options));
        self.filter = (options) => new Locator(injectedScript, selector, options);
        self.first = () => self.locator('nth=0');
        self.last = () => self.locator('nth=-1');
        self.nth = (index) => self.locator(`nth=${index}`);
        self.and = (locator) => new Locator(injectedScript, selectorBase + ` >> internal:and=` + JSON.stringify(locator[selectorSymbol]));
        self.or = (locator) => new Locator(injectedScript, selectorBase + ` >> internal:or=` + JSON.stringify(locator[selectorSymbol]));
    }
}
export class ConsoleAPI {
    _injectedScript;
    constructor(injectedScript) {
        this._injectedScript = injectedScript;
    }
    install() {
        if (this._injectedScript.window.playwright)
            return;
        this._injectedScript.window.playwright = {
            $: (selector, strict) => this._querySelector(selector, !!strict),
            $$: (selector) => this._querySelectorAll(selector),
            inspect: (selector) => this._inspect(selector),
            selector: (element) => this._selector(element),
            generateLocator: (element, language) => this._generateLocator(element, language),
            ariaSnapshot: (element, options) => {
                return this._injectedScript.ariaSnapshot(element || this._injectedScript.document.body, options || { mode: 'default' });
            },
            resume: () => this._resume(),
            ...new Locator(this._injectedScript, ''),
        };
        delete this._injectedScript.window.playwright.filter;
        delete this._injectedScript.window.playwright.first;
        delete this._injectedScript.window.playwright.last;
        delete this._injectedScript.window.playwright.nth;
        delete this._injectedScript.window.playwright.and;
        delete this._injectedScript.window.playwright.or;
    }
    _querySelector(selector, strict) {
        if (typeof selector !== 'string')
            throw new Error(`Usage: playwright.query('Playwright >> selector').`);
        const parsed = this._injectedScript.parseSelector(selector);
        return this._injectedScript.querySelector(parsed, this._injectedScript.document, strict);
    }
    _querySelectorAll(selector) {
        if (typeof selector !== 'string')
            throw new Error(`Usage: playwright.$$('Playwright >> selector').`);
        const parsed = this._injectedScript.parseSelector(selector);
        return this._injectedScript.querySelectorAll(parsed, this._injectedScript.document);
    }
    _inspect(selector) {
        if (typeof selector !== 'string')
            throw new Error(`Usage: playwright.inspect('Playwright >> selector').`);
        this._injectedScript.window.inspect(this._querySelector(selector, false));
    }
    _selector(element) {
        if (!(element instanceof Element))
            throw new Error(`Usage: playwright.selector(element).`);
        return this._injectedScript.generateSelectorSimple(element);
    }
    _generateLocator(element, language) {
        if (!(element instanceof Element))
            throw new Error(`Usage: playwright.locator(element).`);
        const selector = this._injectedScript.generateSelectorSimple(element);
        return asLocator(language || 'javascript', selector);
    }
    _resume() {
        if (!this._injectedScript.window.__pw_resume)
            return false;
        this._injectedScript.window.__pw_resume().catch(() => { });
    }
}
//# sourceMappingURL=consoleApi.js.map