"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 __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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnresolvedPipe = exports.Pipe = void 0;
const uuid_1 = require("uuid");
const element_js_1 = require("../element.js");
const util = __importStar(require("../util/index.js"));
const unit_js_1 = require("./unit.js");
class Pipe extends unit_js_1.Unit {
    constructor() {
        super(...arguments);
        this.id = (0, uuid_1.v4)();
        this.label = '';
        this.notes = '';
        this.enabled = true;
    }
    get $entityType() { return 'pipe'; }
    get type() { return this.$class.$type; }
    get $class() { return this.constructor; }
    get $index() { return this.$owner.indexOf(this); }
    get $key() { return `items/${this.$index}`; }
    get $action() { return this.$owner.$action; }
    init(spec = {}) {
        const { id = (0, uuid_1.v4)(), enabled = true, label = '', notes = '' } = spec;
        this.id = id;
        this.enabled = enabled;
        this.label = label;
        this.notes = notes;
        this.readParams(spec);
    }
    getDepth() {
        return this.$owner.getDepth();
    }
    getPreviousPipe() {
        return this.$owner.previousSibling(this);
    }
    getNextPipe() {
        return this.$owner.nextSibling(this);
    }
    getParentPipe() {
        return this.$owner.$owner instanceof Pipe ? this.$owner.$owner : null;
    }
    *collectPipelines() {
        for (const param of this.getParams()) {
            if (param.type === 'pipeline') {
                yield this[param.name];
            }
        }
    }
    *ancestorPipes() {
        const parent = this.getParentPipe();
        if (parent) {
            yield parent;
            yield* parent.ancestorPipes();
        }
    }
    async map(list, mapFn) {
        const result = [];
        for (const el of list) {
            const res = await mapFn(el);
            if (res == null) {
                continue;
            }
            const newEls = Array.isArray(res) ? res : [res];
            for (const newEl of newEls) {
                util.assertPlayback(newEl instanceof element_js_1.Element, 'Pipes should return Element instances');
                result.push(newEl);
            }
        }
        return result;
    }
}
exports.Pipe = Pipe;
Pipe.$icon = '';
class UnresolvedPipe extends Pipe {
    constructor() {
        super(...arguments);
        this.$originalSpec = null;
    }
    init(spec = {}) {
        super.init(spec);
        this.$originalSpec = spec;
    }
    toJSON() {
        return this.$originalSpec;
    }
    async apply() {
        const { type } = this.$originalSpec || {};
        throw util.scriptError(`Cannot run unresolved pipe: ${type}`);
    }
}
exports.UnresolvedPipe = UnresolvedPipe;
UnresolvedPipe.$type = 'unresolved';
UnresolvedPipe.$hidden = true;
//# sourceMappingURL=pipe.js.map