import {
  findInstance,
  registerInstanceWithMeta
} from "./chunk-4QIJY5PM.js";

// src/typeclasses/flatmap.ts
import { createGenericRegistry } from "@typesugar/core";
var flatMapArray = {
  map: (fa, f) => fa.map(f),
  flatMap: (fa, f) => fa.flatMap(f)
};
var flatMapPromise = {
  map: (fa, f) => fa.then(f),
  flatMap: (fa, f) => fa.then(f)
};
var flatMapIterable = {
  map: (fa, f) => iterableMap(fa, f),
  flatMap: (fa, f) => iterableFlatMap(fa, f)
};
var flatMapAsyncIterable = {
  map: (fa, f) => asyncIterableMap(fa, f),
  flatMap: (fa, f) => asyncIterableFlatMap(fa, f)
};
function* iterableMap(fa, f) {
  for (const a of fa) {
    yield f(a);
  }
}
function* iterableFlatMap(fa, f) {
  for (const a of fa) {
    yield* f(a);
  }
}
async function* asyncIterableMap(fa, f) {
  for await (const a of fa) {
    yield f(a);
  }
}
async function* asyncIterableFlatMap(fa, f) {
  for await (const a of fa) {
    yield* f(a);
  }
}
var flatMapInstances = createGenericRegistry({
  name: "FlatMapRegistry",
  duplicateStrategy: "replace"
});
flatMapInstances.set("Array", flatMapArray);
flatMapInstances.set("Promise", flatMapPromise);
flatMapInstances.set("Iterable", flatMapIterable);
flatMapInstances.set("AsyncIterable", flatMapAsyncIterable);
function registerInUnifiedRegistry(name, meta) {
  registerInstanceWithMeta({
    typeclassName: "FlatMap",
    forType: name,
    instanceName: `flatMap${name}`,
    derived: false,
    meta
  });
}
registerInUnifiedRegistry("Array");
registerInUnifiedRegistry("Promise", {
  methodNames: { bind: "then", map: "then", orElse: "catch" }
});
registerInUnifiedRegistry("Iterable");
registerInUnifiedRegistry("AsyncIterable");
function registerFlatMap(name, instance, meta) {
  if (flatMapInstances.has(name)) {
    console.warn(`[typesugar] FlatMap instance for '${name}' is already registered. Overriding.`);
  }
  flatMapInstances.set(name, instance);
  registerInUnifiedRegistry(name, meta);
}
function getFlatMap(name) {
  return flatMapInstances.get(name);
}
function hasFlatMap(name) {
  return findInstance("FlatMap", name) !== void 0;
}

export {
  flatMapArray,
  flatMapPromise,
  flatMapIterable,
  flatMapAsyncIterable,
  registerFlatMap,
  getFlatMap,
  hasFlatMap
};
//# sourceMappingURL=chunk-4LPO3PYW.js.map