import * as Context from "effect/Context";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as ConfigLoader from "./ConfigLoader.js";
import * as Model from "./Model.js";
export class ManifestContextError extends Data.TaggedError("ManifestContextError") {
}
export const ManifestContext = Context.GenericTag("Amp/ManifestContext");
/**
 * Parse all dependencies from a manifest's dependencies record.
 */
export const parseDependencies = (dependencies) => {
    return Effect.forEach(Object.entries(dependencies), ([_key, refStr]) => Model.parseDatasetReference(refStr), { concurrency: "unbounded" });
};
const fromConfigFile = (file) => Effect.gen(function* () {
    const loader = yield* ConfigLoader.ConfigLoader;
    const buildResult = yield* loader.build(file).pipe(Effect.mapError((cause) => new ManifestContextError({ cause, file, message: "Failed to build config file" })));
    return {
        metadata: buildResult.metadata,
        manifest: buildResult.manifest,
        dependencies: buildResult.dependencies,
    };
});
export const layerFromConfigFile = (file) => Option.match(file, {
    onSome: (file) => fromConfigFile(file),
    onNone: () => Effect.gen(function* () {
        const configLoader = yield* ConfigLoader.ConfigLoader;
        const foundConfigPath = yield* configLoader.find();
        if (Option.isNone(foundConfigPath)) {
            return yield* new ManifestContextError({ message: "Failed to find config file" });
        }
        return yield* fromConfigFile(foundConfigPath.value);
    }),
}).pipe(Effect.map((context) => Context.make(ManifestContext, context)), Layer.effectContext, Layer.provide(ConfigLoader.ConfigLoader.Default));
//# sourceMappingURL=ManifestContext.js.map