"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OptionsBuilder = exports.BuildData = void 0;
exports.buildData = buildData;
exports.builder = builder;
exports.renderToFigure = renderToFigure;
exports.renderToString = renderToString;
exports.text = text;
exports.text_ = text_;
exports.toBuildData = toBuildData;
exports.toRenderOptions = toRenderOptions;
exports.withDefaultFont = withDefaultFont;
exports.withDefaultHorizontalLayout = withDefaultHorizontalLayout;
exports.withDefaultJustification = withDefaultJustification;
exports.withDefaultMaxWidth = withDefaultMaxWidth;
exports.withDefaultPrintDirection = withDefaultPrintDirection;
exports.withFigFont = withFigFont;
exports.withFigFont_ = withFigFont_;
exports.withFont = withFont;
exports.withFont_ = withFont_;
exports.withHorizontalLayout = withHorizontalLayout;
exports.withHorizontalLayout_ = withHorizontalLayout_;
exports.withInitialAction = withInitialAction;
exports.withInternalFont = withInternalFont;
exports.withInternalFont_ = withInternalFont_;
exports.withJustification = withJustification;
exports.withJustification_ = withJustification_;
exports.withMaxWidth = withMaxWidth;
exports.withMaxWidth_ = withMaxWidth_;
exports.withPrintDirection = withPrintDirection;
exports.withPrintDirection_ = withPrintDirection_;

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

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

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

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

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

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

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

var HL = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../RenderOptions/HorizontalLayout/index.js"));

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

var J = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../RenderOptions/Justification/index.js"));

var PD = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../RenderOptions/PrintDirection/index.js"));

var Actions = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./BuilderAction/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; }

// ets_tracing: off
// -----------------------------------------------------------------------------
// Model
// -----------------------------------------------------------------------------

/**
 * Represents a collection of rendering options.
 *
 * This builder works by recording what settings a user wants to use instead of
 * applying the setting immediately when calling a method. This allows for a
 * fail-safe behaviour when, for instance, a user wants to load a file but the
 * file is missing. Instead of receiving an exception when calling
 * `OptionsBuilder.withFont`, the builder will simply record the desire of to
 * load a file. The actual loading, and failure, will happen and be handled when
 * calling `OptionsBuilder.compile()`.
 */
class OptionsBuilder {
  constructor(
  /**
   * The list of actions that will be executed to obtain the final
   * configuration.
   */
  actions = C.empty()) {
    this.actions = actions;
  }

}

exports.OptionsBuilder = OptionsBuilder;

class BuildData extends _Case.Case {} // -----------------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------------

/**
 * Creates a new `OptionsBuilder` with default settings.
 */


exports.BuildData = BuildData;

function builder() {
  return new OptionsBuilder();
}
/**
 * Creates a new `OptionsBuilder` with default settings and using an initial
 * action.
 */


function withInitialAction(initialAction) {
  return new OptionsBuilder(C.single(initialAction));
}
/**
 * Create a new instance of `BuildData` using the provided parameters.
 */


function buildData(params = {}) {
  return new BuildData({
    font: O.none,
    horizontalLayout: HL.FontDefault,
    justification: J.FontDefault,
    maxWidth: O.none,
    printDirection: PD.FontDefault,
    text: "",
    ...params
  });
} // -----------------------------------------------------------------------------
// Combinators
// -----------------------------------------------------------------------------

/**
 * Set the text to render.
 */


function text_(self, text) {
  return addAction(self, new Actions.SetText({
    text
  }));
}
/**
 * Set the text to render.
 *
 * @ets_data_first text_
 */


function text(text) {
  return self => text_(self, text);
}
/**
 * Use the default `FigFont` to render the text.
 */


function withDefaultFont(self) {
  return addAction(self, new Actions.DefaultFont());
}
/**
 * Use one of the internal fonts to render the text.
 *
 * The loading of the font is performed when the `RenderOptions` is built.
 */


function withInternalFont_(self, font) {
  return addAction(self, new Actions.LoadInternalFont({
    font
  }));
}
/**
 * Use one of the internal fonts to render the text.
 *
 * The loading of the font is performed when the `RenderOptions` is built.
 *
 * @ets_data_first withInternalFont_
 */


function withInternalFont(font) {
  return self => withInternalFont_(self, font);
}
/**
 * Use a font loaded from a Figlet file to render the text.
 *
 * The loading of the font is performed when the `RenderOptions` is built.
 */


function withFont_(self, fontPath) {
  return addAction(self, new Actions.LoadFont({
    fontPath
  }));
}
/**
 * Use a font loaded from a Figlet file to render the text.
 *
 * The loading of the font is performed when the `RenderOptions` is built.
 *
 * @ets_data_first withFont_
 */


function withFont(fontPath) {
  return self => withFont_(self, fontPath);
}
/**
 * Use a font that has already been loaded to render the text.
 */


function withFigFont_(self, font) {
  return addAction(self, new Actions.SetFont({
    font
  }));
}
/**
 * Use a font that has already been loaded to render the text.
 *
 * @ets_data_first withFigFont_
 */


function withFigFont(font) {
  return self => withFigFont_(self, font);
}
/**
 * Use the default horizontal layout to render the text.
 */


function withDefaultHorizontalLayout(self) {
  return addAction(self, new Actions.DefaultHorizontalLayout());
}
/**
 * Use the specified `HorizontalLayout` to render the text.
 */


function withHorizontalLayout_(self, layout) {
  return addAction(self, new Actions.SetHorizontalLayout({
    layout
  }));
}
/**
 * Use the specified `HorizontalLayout` to render the text.
 *
 * @ets_data_first withHorizontalLayout_
 */


function withHorizontalLayout(layout) {
  return self => withHorizontalLayout_(self, layout);
}
/**
 * Use the default maximum width to render the text.
 */


function withDefaultMaxWidth(self) {
  return addAction(self, new Actions.DefaultMaxWidth());
}
/**
 * Use the specified maximum width to render the text.
 */


function withMaxWidth_(self, maxWidth) {
  return addAction(self, new Actions.SetMaxWidth({
    maxWidth
  }));
}
/**
 * Use the specified maximum width to render the text.
 *
 * @ets_data_first withMaxWidth_
 */


function withMaxWidth(maxWidth) {
  return self => withMaxWidth_(self, maxWidth);
}
/**
 * Use the default print direction to render the text.
 *
 * **NOTE**: This feature is not yet implemented and will have no effect.
 */


function withDefaultPrintDirection(self) {
  return addAction(self, new Actions.DefaultPrintDirection());
}
/**
 * Use the specified print direction to render the text.
 *
 * **NOTE**: This feature is not yet implemented and will have no effect.
 */


function withPrintDirection_(self, direction) {
  return addAction(self, new Actions.SetPrintDirection({
    direction
  }));
}
/**
 * Use the specified print direction to render the text.
 *
 * **NOTE**: This feature is not yet implemented and will have no effect.
 *
 * @ets_data_first withPrintDirection_
 */


function withPrintDirection(direction) {
  return self => withPrintDirection_(self, direction);
}
/**
 * Use the default justification to render the text.
 *
 * **NOTE**: This feature is not yet implemented and will have no effect.
 */


function withDefaultJustification(self) {
  return addAction(self, new Actions.DefaultJustification());
}
/**
 * Use the specified justification to render the text.
 *
 * **NOTE**: This feature is not yet implemented and will have no effect.
 */


function withJustification_(self, justification) {
  return addAction(self, new Actions.SetJustification({
    justification
  }));
}
/**
 * Use the specified justification to render the text.
 *
 * **NOTE**: This feature is not yet implemented and will have no effect.
 *
 * @ets_data_first withJustification_
 */


function withJustification(justification) {
  return self => withJustification_(self, justification);
} // -----------------------------------------------------------------------------
// Destructors
// -----------------------------------------------------------------------------


function toBuildData(self) {
  return T.chain_(FigletClient.defaultFont, defaultFont => C.reduceEffect_(self.actions, buildData(), (data, action) => {
    return T.matchTag({
      // Fonts
      DefaultFont: () => T.map_(FigletClient.loadFontInternal(defaultFont), font => data.copy({
        font: O.some(font)
      })),
      SetFont: _ => T.succeed(data.copy({
        font: O.some(_.font)
      })),
      LoadFont: _ => T.map_(FigletClient.loadFont(_.fontPath), font => data.copy({
        font: O.some(font)
      })),
      LoadInternalFont: _ => T.map_(FigletClient.loadFontInternal(_.font), font => data.copy({
        font: O.some(font)
      })),
      // Horizontal Layout
      DefaultHorizontalLayout: () => T.succeed(data.copy({
        horizontalLayout: HL.FontDefault
      })),
      SetHorizontalLayout: _ => T.succeed(data.copy({
        horizontalLayout: _.layout
      })),
      // Justification
      DefaultJustification: () => T.succeed(data.copy({
        justification: J.FontDefault
      })),
      SetJustification: _ => T.succeed(data.copy({
        justification: _.justification
      })),
      // Max Width
      DefaultMaxWidth: () => T.succeed(data.copy({
        maxWidth: O.none
      })),
      SetMaxWidth: _ => T.succeed(data.copy({
        maxWidth: O.some(_.maxWidth)
      })),
      // Print Direction
      DefaultPrintDirection: () => T.succeed(data.copy({
        printDirection: PD.FontDefault
      })),
      SetPrintDirection: _ => T.succeed(data.copy({
        printDirection: _.direction
      })),
      // Text
      SetText: _ => T.succeed(data.copy({
        text: _.text
      }))
    })(action);
  }));
}

function toRenderOptions(self) {
  return T.map_(T.bind_(T.bind_(T.bind_(T.do, "options", () => toBuildData(self)), "font", ({
    options
  }) => O.fold_(options.font, () => T.chain_(FigletClient.defaultFont, FigletClient.loadFontInternal), T.succeed)), "maxWidth", ({
    options
  }) => O.fold_(options.maxWidth, () => FigletClient.defaultMaxWidth, T.succeed)), ({
    font,
    maxWidth,
    options
  }) => new _index4.RenderOptions({
    font,
    maxWidth,
    horizontalLayout: options.horizontalLayout,
    justification: options.justification,
    printDirection: options.printDirection
  }));
}

function renderToFigure(self) {
  return T.map_(T.bind_(T.bind_(T.do, "buildOptions", () => toBuildData(self)), "renderOptions", () => toRenderOptions(self)), ({
    buildOptions,
    renderOptions
  }) => FigletClient.renderToFigure(buildOptions.text, renderOptions));
}

function renderToString(self) {
  return T.map_(renderToFigure(self), Fig.showFigure.show);
} // -----------------------------------------------------------------------------
// Utilities
// -----------------------------------------------------------------------------


function addAction(self, action) {
  return new OptionsBuilder(C.prepend_(C.filter_(self.actions, (0, _Function.not)(Actions.sameGroupAs(action))), action));
}
//# sourceMappingURL=index.js.map