"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.makeFigletClient = exports.loadFontInternal = exports.loadFont = exports.internalFonts = exports.defaultMaxWidth = exports.defaultFont = exports.LiveFigletClient = exports.FigletClientId = exports.FigletClient = void 0;
exports.renderToFigure = renderToFigure;
exports.renderToString = renderToString;

var C = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Collections/Immutable/Chunk"));

var NA = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Collections/Immutable/NonEmptyArray"));

var T = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect"));

var L = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect/Layer"));

var S = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect/Stream"));

var Sink = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect/Stream/Sink"));

var Transducer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect/Stream/Transducer"));

var _Function = /*#__PURE__*/require("@effect-ts/core/Function");

var _Has = /*#__PURE__*/require("@effect-ts/core/Has");

var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Option"));

var NodeJSFileSystem = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("fs"));

var NodeJSPath = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("path"));

var FF = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../FigFont/index.js"));

var _index2 = /*#__PURE__*/require("../FigletException/index.js");

var Fig = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Figure/index.js"));

var _index4 = /*#__PURE__*/require("../FontFileReader/index.js");

var _index5 = /*#__PURE__*/require("../Internal/Transducers/index.js");

var Rendering = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Rendering/index.js"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// -----------------------------------------------------------------------------
// Figlet Client
// -----------------------------------------------------------------------------
const FigletClientId = /*#__PURE__*/Symbol();
exports.FigletClientId = FigletClientId;
const makeFigletClient = /*#__PURE__*/T.gen(function* (_) {
  const {
    read
  } = yield* _(_index4.FontFileReader);
  const fontsPath = NodeJSPath.join(__dirname, "..", "Internal", "fonts");
  return (0, _Has.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 _index2.FigletFileError({
      message: `Unable to locate internal font file for '${name}'`
    }))), filepath => read(filepath, createFigFont)))
  });
});
exports.makeFigletClient = makeFigletClient;
const FigletClient = /*#__PURE__*/(0, _Has.tag)(FigletClientId);
exports.FigletClient = FigletClient;
const LiveFigletClient = /*#__PURE__*/L.fromEffect_(makeFigletClient, FigletClient);
exports.LiveFigletClient = LiveFigletClient;
const {
  defaultFont,
  defaultMaxWidth,
  internalFonts,
  loadFont,
  loadFontInternal
} = /*#__PURE__*/T.deriveLifted(FigletClient)(["loadFont", "loadFontInternal"], ["internalFonts"], ["defaultFont", "defaultMaxWidth"]);
exports.loadFontInternal = loadFontInternal;
exports.loadFont = loadFont;
exports.internalFonts = internalFonts;
exports.defaultMaxWidth = defaultMaxWidth;
exports.defaultFont = defaultFont;

function createFigFont(file, buffer) {
  const transducer = Transducer.andThen(_index5.splitLines)(_index5.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`.
 */


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


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 _index2.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.js.map