import * as C from "@effect-ts/core/Collections/Immutable/Chunk";
import * as NA from "@effect-ts/core/Collections/Immutable/NonEmptyArray";
import * as T from "@effect-ts/core/Effect";
import * as L from "@effect-ts/core/Effect/Layer";
import * as S from "@effect-ts/core/Effect/Stream";
import * as Sink from "@effect-ts/core/Effect/Stream/Sink";
import * as Transducer from "@effect-ts/core/Effect/Stream/Transducer";
import { pipe } from "@effect-ts/core/Function";
import { service, tag } from "@effect-ts/core/Has";
import * as O from "@effect-ts/core/Option";
import * as NodeJSFileSystem from "fs";
import * as NodeJSPath from "path";
import * as FF from "../FigFont/index.mjs";
import { FigletFileError } from "../FigletException/index.mjs";
import * as Fig from "../Figure/index.mjs";
import { FontFileReader } from "../FontFileReader/index.mjs";
import { splitLines, utf8Decode } from "../Internal/Transducers/index.mjs";
import * as Rendering from "../Rendering/index.mjs"; // -----------------------------------------------------------------------------
// Figlet Client
// -----------------------------------------------------------------------------

export const FigletClientId = /*#__PURE__*/Symbol();
export const makeFigletClient = /*#__PURE__*/T.gen(function* (_) {
  const {
    read
  } = yield* _(FontFileReader);
  const fontsPath = NodeJSPath.join(__dirname, "..", "Internal", "fonts");
  return service({
    defaultFont: "standard",
    defaultMaxWidth: 80,
    internalFonts: T.map_(listFontFiles(fontsPath), C.map(file => NodeJSPath.basename(file).replace(".flf", ""))),
    loadFont: path => read(path, createFigFont),
    loadFontInternal: name => T.chain_(T.mapError_(listFontFiles(fontsPath), NA.single), files => O.fold_(C.find_(files, file => NodeJSPath.basename(file) === `${name}.flf`), () => T.fail(NA.single(new FigletFileError({
      message: `Unable to locate internal font file for '${name}'`
    }))), filepath => read(filepath, createFigFont)))
  });
});
export const FigletClient = /*#__PURE__*/tag(FigletClientId);
export const LiveFigletClient = /*#__PURE__*/L.fromEffect_(makeFigletClient, FigletClient);
export const {
  defaultFont,
  defaultMaxWidth,
  internalFonts,
  loadFont,
  loadFontInternal
} = /*#__PURE__*/T.deriveLifted(FigletClient)(["loadFont", "loadFontInternal"], ["internalFonts"], ["defaultFont", "defaultMaxWidth"]);

function createFigFont(file, buffer) {
  const transducer = Transducer.andThen(splitLines)(utf8Decode);
  return T.chain_(T.mapError_(S.run(Sink.collectAll())(S.aggregate(transducer)(buffer)), NA.single), lines => T.fromEither(() => FF.fromFile(file, lines)));
} // -----------------------------------------------------------------------------
// Operations
// -----------------------------------------------------------------------------

/**
 * Render the specified text to a `Figure` using the provided `RenderOptions`.
 */


export function renderToFigure(text, options) {
  return Rendering.render(text, options);
}
/**
 * Render the specified text to a string using the provided `RenderOptions`.
 */

export function renderToString(text, options) {
  return Fig.showFigure.show(renderToFigure(text, options));
} // -----------------------------------------------------------------------------
// Utils
// -----------------------------------------------------------------------------

function listFontFiles(path) {
  return T.effectAsync(resume => {
    NodeJSFileSystem.readdir(path, {
      withFileTypes: true
    }, (err, dirents) => {
      if (err) {
        resume(T.fail(new FigletFileError({
          message: err.message
        })));
      } else {
        resume(T.map_(C.forEachF(T.Applicative)(dirent => {
          const fullPath = NodeJSPath.resolve(path, dirent.name);
          return dirent.isDirectory() ? listFontFiles(fullPath) : dirent.name.endsWith(".flc") ? T.succeed(C.empty()) : T.succeed(C.single(fullPath));
        })(C.from(dirents)), C.flatten));
      }
    });
  });
}
//# sourceMappingURL=index.mjs.map