"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transformer = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const array_1 = require("./array");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2,\"typeName\":\"(input: I[]) =\\u003e O[] | Promise\\u003cO[]\\u003e\"},{\"kind\":2},{\"kind\":2,\"typeName\":\"(input: I) =\\u003e O\"},{\"kind\":2,\"typeName\":\"(input: I) =\\u003e Promise\\u003cO\\u003e\"},{\"kind\":2,\"typeName\":\"K\"},{\"kind\":22,\"type\":{\"kind\":14,\"type\":{\"kind\":2}}},{\"kind\":22,\"type\":{\"kind\":14,\"type\":{\"kind\":2,\"typeName\":\"I\"}}}]]", []);
let _TransformerMetadata_1;
class Transformer {
    input;
    executor;
    static create(input) {
        return new Transformer(input);
    }
    constructor(input) {
        this.input = input;
    }
    apply(fn, shouldApply = true) {
        this.executor = shouldApply ? input => fn(input) : input => input;
        return new Transformer(this);
    }
    applyEach(fn, shouldApply = true) {
        return this.apply(items => items.map(fn), shouldApply);
    }
    applyEachAsync(fn, shouldApply = true) {
        return this.apply(items => (0, array_1.asyncMap)(items, fn), shouldApply);
    }
    narrow(...keys) {
        return this.apply(items => items.map(item => pick(item, keys)));
    }
    async execute() {
        if (!this.executor)
            throw new Error('No executor defined');
        const input = await this.get();
        return this.executor(input);
    }
    async get() {
        if (this.input instanceof Transformer)
            return (await this.input.execute());
        return this.input;
    }
    static __tsfType = _TransformerMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"Transformer\",\"typeName\":\"Transformer\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"apply\",\"parameters\":[{\"name\":\"fn\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false},{\"name\":\"shouldApply\",\"type\":{\"$tsfType\":1},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":16,\"typeName\":\"Transformer\",\"classType\":{\"$tsf\":1},\"typeArguments\":[{\"kind\":2,\"typeName\":\"O\"}]}},{\"name\":\"applyEach\",\"parameters\":[{\"name\":\"fn\",\"type\":{\"$tsfType\":2},\"optional\":false,\"default\":false},{\"name\":\"shouldApply\",\"type\":{\"$tsfType\":1},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":16,\"typeName\":\"Transformer\",\"classType\":{\"$tsf\":1},\"typeArguments\":[{\"kind\":2,\"typeName\":\"O\"}]}},{\"name\":\"applyEachAsync\",\"parameters\":[{\"name\":\"fn\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false},{\"name\":\"shouldApply\",\"type\":{\"$tsfType\":1},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":16,\"typeName\":\"Transformer\",\"classType\":{\"$tsf\":1},\"typeArguments\":[{\"kind\":2,\"typeName\":\"O\"}]}},{\"name\":\"narrow\",\"parameters\":[{\"name\":\"keys\",\"type\":{\"$tsfType\":4},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":16,\"typeName\":\"Transformer\",\"classType\":{\"$tsf\":1},\"typeArguments\":[{\"kind\":18,\"typeName\":\"Pick\",\"utilityType\":\"Pick\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}],\"utilityKeys\":[\"K[number]\"],\"types\":[]}]}},{\"name\":\"execute\",\"parameters\":[],\"returnType\":{\"$tsfType\":5}},{\"name\":\"get\",\"parameters\":[],\"returnType\":{\"$tsfType\":6}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"input\",\"type\":{\"kind\":12,\"types\":[{\"kind\":14,\"type\":{\"kind\":2,\"typeName\":\"I\"}},{\"kind\":16,\"typeName\":\"Transformer\",\"classType\":{\"$tsf\":1},\"typeArguments\":[{\"kind\":2}]}]},\"optional\":false,\"default\":false}]}]", [() => Transformer, () => (typeof Transformer !== "undefined" ? Transformer : (typeof exports !== "undefined" ? exports.Transformer : undefined))], __tsf_metadata_type);
}
exports.Transformer = Transformer;
_TransformerMetadata_1.classType = Transformer;
Transformer.__tsfType = _TransformerMetadata_1;
function pick(item, keys) {
    const result = {};
    for (const key of keys)
        result[key] = item[key];
    return result;
}
//# sourceMappingURL=transformer.js.map