"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.render = render;

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

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

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

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

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

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

var _index6 = /*#__PURE__*/require("./MergeStrategy/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
// -----------------------------------------------------------------------------
// Rendering Functions
// -----------------------------------------------------------------------------

/**
 * Renders text into a `Figure` for a given `FigFont` and options
 *
 *
 * Explanation of the general algorithm with final `overlap = 3`, horizontal
 * fitting layout and print direction left-to-right.
 *
 * Example merged `Figure`s (using `HorizontalFitting` as example renderer):
 *
 * ```text
 *    Figure A   Figure B        Resulting Figure
 *   /        \ /       \       /               \
 *   +-----+---+---+-----+      +-----+---+-----+
 *   |  ___|__ |   |     |      |  ___|__ |     |
 *   | |  _|__||   |__ _ |      | |  _|__||__ _ |
 *   | | |_|   |  /| _` ||  ->  | | |_|  /| _` ||
 *   | |  _||  | | |(_| ||  ->  | |  _||| |(_| ||
 *   | |_| |   |  \|__,_||      | |_| |  \|__,_||
 *   |     |   |   |     |      |     |   |     |
 *   +-----+---+---+-----+      +-----+---+-----+
 *          \     /                     |
 *       Overlap area            Merged 3 columns
 *       3 cols each
 * ```
 *
 * In this example each `Figure` is broken down in `SubColumns` with final
 * `overlap = 3`:
 *
 * ```text
 * Figure A                                          | A-overlapping |
 * +--------+           +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+
 * |  _____ |           | |   | |   |_|   |_|   |_|   |_|   |_|   | |
 * | |  ___||           | |   |||   | |   | |   |_|   |_|   |_|   |||
 * | | |_   |       ->  | | + ||| + | | + ||| + |_| + | | + | | + | |
 * | |  _|  |       ->  | | + ||| + | | + | | + |_| + ||| + | | + | |
 * | |_|    |           | |   |||   |_|   |||   | |   | |   | |   | |
 * |        |           | |   | |   | |   | |   | |  /| |   | |   | |
 * +--------+           +-+   +-+   +-+   +-+   +-+ / +-+   +-+   +-+
 *                                                 / |               |
 *                                 A active column   |               |-- Final overlap = 3 columns
 * Resulting Figure                                  |               |
 * +-------------+      +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+
 * |  _____      |      | |   | |   |_|   |_|   |_|   |_|   |_|   | |   | |   | |   | |   | |   | |
 * | |  ___|__ _ |      | |   |||   | |   | |   |_|   |_|   |_|   |||   |_|   |_|   | |   |_|   | |
 * | | |_  / _` ||  ->  | | + ||| + | | + ||| + |_| + | | + | | + |/| + | | + |_| + |`| + | | + |||
 * | |  _|| (_| ||  ->  | | + ||| + | | + | | + |_| + ||| + ||| + | | + |(| + |_| + ||| + | | + |||
 * | |_|   \__,_||      | |   |||   |_|   |||   | |   | |   | |   |\|   |_|   |_|   |,|   |_|   |||
 * |             |      | |   | |   | |   | |   | |   | |   | |   | |   | |   | |   | |   | |   | |
 * +-------------+      +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+
 *                                                   |              |
 *                                 B active column   |              |
 * Figure B                                        \ |              |
 * +--------+                                       \ +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+
 * |        |                                        \| |   | |   | |   | |   | |   | |   | |   | |
 * |   __ _ |                                         | |   | |   | |   |_|   |_|   | |   |_|   | |
 * |  / _` ||      ->                                 | | + | | + |/| + | | + |_| + |`| + | | + |||
 * | | (_| ||      ->                                 | | + ||| + | | + |(| + |_| + ||| + | | + |||
 * |  \__,_||                                         | |   | |   |\|   |_|   |_|   |,|   |_|   |||
 * |        |                                         | |   | |   | |   | |   | |   | |   | |   | |
 * +--------+                                         +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+
 *                                                   | B-overlapping |
 * ```
 *
 * Merge of a single overlapping column with the custom merge function `f`:
 *
 * ```text
 * +-+     +-+                                                 +-+
 * |_|  +  | |  ->  f('_', ' ') = Continue('_')                |_|
 * |_|  +  | |  ->  f('_', ' ') = Continue('_')                |_|
 * | |  +  | |  ->  f(' ', ' ') = Continue(' ')  ->  Continue( | | )
 * | |  +  |||  ->  f(' ', '|') = Continue('|')                |||
 * | |  +  | |  ->  f(' ', ' ') = Continue(' ')                | |
 * | |  +  | |  ->  f(' ', ' ') = Continue('_')                |_|
 * +-+     +-+                                                 +-+
 * ```
 *
 * NOTES:
 * - Each recursive iteration works with a certain amount of overlapping columns.
 *   The entire overlapping area is merged but it's only the "active columns"
 *   that decide the outcome of the iteration as the subsequent ones will merge
 *   for sure. Once the overlapping area has been process it decides between
 *   3 options:
 *   - the overlap of the current iteration results in a valid merge, the
 *     overlap can be increased further and thus runs a new iteration with
 *     `overlap + 1`
 *   - the overlap of the current iteration results in a valid merge but the
 *     overlap cannot be increased and returns the result of the current
 *     iteration as the final result
 *   - the overlap of the current iteration does not results in a valid merge
 *     and the result of the previous iteration is used as the final result
 * - At `overlap = n` the `n - 1` overlap values have already passed through the
 *   merge algorithm and their result is assumed to be a valid merge
 * - The "A active column" and the "B active column" (see figures above) are the
 *   columns that decide the result of the iteration
 * - Each pair of corresponding characters of the active columns are passed to a
 *   custom merge function
 * - The custom merge function returns the character resulting from merge of the
 *   two corresponding character together with the decision of how to proceed
 *   with the algorithm
 * - The result value of the custom merge function is an Applicative Functor
 *
 * @param text The string to render as a `Figure`.
 * @param options The `RenderOptions` used to render the text.
 * @return A `Figure` containing the rendered text following the rendering
 * options.
 */
function render(text, options) {
  const figures = C.map_(C.from(text.split("")), char => FigCharacter.columns(FigFont.char_(options.font, char)).value);
  const zero = C.single(SubLines.toSubcolumns(FigFont.zero(options.font).lines).value);
  const result = C.map_((0, _index6.appendLoop)(options, figures, zero, (0, _index6.appendLoopState)()), SubColumns.fromValue);
  return new _index3.Figure({
    font: options.font,
    value: text,
    columns: result
  });
}
//# sourceMappingURL=index.js.map