"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 __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 __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DomBatchExtract = void 0;
const index_js_1 = require("../model/index.js");
const util = __importStar(require("../util/index.js"));
class DomBatchExtract extends index_js_1.Pipe {
    constructor() {
        super(...arguments);
        this.properties = [];
    }
    async apply(inputSet, _ctx) {
        const properties = this.properties;
        const results = [];
        const partitions = util.partitionBy(inputSet, el => el.remote.executionContext.executionContextId);
        for (const partition of partitions) {
            if (!partition.length) {
                continue;
            }
            const executionContext = partition[0].remote.executionContext;
            const remotes = partition.map(_ => _.remote);
            const data = await executionContext.evaluateJson((properties, toolkitBinding, ...els) => {
                const toolkit = window[toolkitBinding];
                const results = [];
                for (const el of els) {
                    const res = {};
                    for (const prop of properties) {
                        const subEl = prop.selector ? el.querySelector(prop.selector) : el;
                        if (subEl) {
                            const info = toolkit.getElementInfo(subEl);
                            const data = prop.property ? info[prop.property] : info;
                            res[prop.key] = data;
                        }
                        else {
                            res[prop.key] = null;
                        }
                    }
                    results.push(res);
                }
                return results;
            }, properties, executionContext.page.toolkitBinding, ...remotes);
            for (let i = 0; i < partition.length; i++) {
                const el = partition[i];
                const extractedData = util.convertObjectPointers(data[i]);
                const newData = util.getType(el.value) === 'object' ? Object.assign({}, el.value, extractedData) : extractedData;
                const newEl = el.clone(newData);
                results.push(newEl);
            }
        }
        return results;
    }
}
DomBatchExtract.$type = 'DOM.batchExtract';
DomBatchExtract.$help = `
Extracts multiple DOM properties from element or its descendants designated by selector at once.

This pipe replaces a series of Set Path > Query One > Extract pipelines,
and is significantly more efficient.
`;
__decorate([
    index_js_1.params.Recordset({
        singular: 'property',
        fields: [
            {
                name: 'key',
                type: 'string',
                value: '',
            },
            {
                name: 'selector',
                type: 'selector',
                value: '',
            },
            {
                name: 'property',
                type: 'enum',
                value: '',
                enum: [
                    '',
                    'text',
                    'innerText',
                    'textContent',
                    'childText',
                    'value',
                    'checked',
                    'disabled',
                    'attributes',
                    'classList',
                ],
            },
        ],
    }),
    __metadata("design:type", Array)
], DomBatchExtract.prototype, "properties", void 0);
exports.DomBatchExtract = DomBatchExtract;
//# sourceMappingURL=DOM.batchExtract.js.map