"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectTargets = void 0;
const url_1 = __importDefault(require("url"));
const element_js_1 = require("../element.js");
const index_js_1 = require("../model/index.js");
class SelectTargets extends index_js_1.Pipe {
    constructor() {
        super(...arguments);
        this.targetType = 'page';
        this.hostnameRegexp = '.*';
        this.pathnameRegexp = '.*';
    }
    async apply(inputSet, _ctx) {
        const pages = await this.collectPages();
        const results = [];
        for (const el of inputSet) {
            for (const page of pages) {
                const document = await page.document();
                results.push(new element_js_1.Element(document, el.value));
            }
        }
        return results;
    }
    async collectPages() {
        const targetType = this.targetType;
        const hostnameRegexp = this.hostnameRegexp;
        const pathnameRegexp = this.pathnameRegexp;
        const pages = [];
        const targets = [...this.$browser.attachedTargets()];
        for (const target of targets) {
            const { url, type } = target;
            if (type !== targetType || !target.isValidTarget()) {
                continue;
            }
            const { hostname, pathname } = url_1.default.parse(url);
            if (!new RegExp(`^${hostnameRegexp}$`).test(hostname || '')) {
                continue;
            }
            if (!new RegExp(`^${pathnameRegexp}$`).test(pathname || '')) {
                continue;
            }
            const page = await target.getPage();
            pages.push(page);
        }
        return pages;
    }
}
SelectTargets.$type = 'Browser.selectTargets';
SelectTargets.$help = `
Returns information about all currently open browser targets (tabs, popups, windows, iframes),
matching specified criteria.
`;
__decorate([
    index_js_1.params.Enum({ enum: ['page', 'iframe'] }),
    __metadata("design:type", String)
], SelectTargets.prototype, "targetType", void 0);
__decorate([
    index_js_1.params.String(),
    __metadata("design:type", String)
], SelectTargets.prototype, "hostnameRegexp", void 0);
__decorate([
    index_js_1.params.String(),
    __metadata("design:type", String)
], SelectTargets.prototype, "pathnameRegexp", void 0);
exports.SelectTargets = SelectTargets;
//# sourceMappingURL=Browser.selectTargets.js.map