"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ControlledSmushing = ControlledSmushing;
exports.equalHorizontalLayout = exports.UniversalSmushing = exports.HorizontalFitting = exports.FullWidth = void 0;
exports.fromFullLayout = fromFullLayout;
exports.fromHeader = fromHeader;
exports.fromOldLayout = fromOldLayout;

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

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 E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Either"));

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

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

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

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

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

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

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

var HSR = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../HorizontalSmushingRule/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
// -----------------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------------
const FullWidth = {
  _tag: "FullWidth"
};
exports.FullWidth = FullWidth;
const HorizontalFitting = {
  _tag: "HorizontalFitting"
};
exports.HorizontalFitting = HorizontalFitting;
const UniversalSmushing = {
  _tag: "UniversalSmushing"
};
exports.UniversalSmushing = UniversalSmushing;

function ControlledSmushing(rules) {
  return {
    _tag: "ControlledSmushing",
    rules
  };
}
/**
 * Interprets the header settings and returns the selected `HorizontalLayout`.
 * Contrary to the Figlet font standard, if the header defines "fullLayout"
 * and then defines "oldLayout", "oldLayout" is effectively ignored.
 *
 * @param header The `FigHeader` where the `HorizontalLayout` is defined.
 * @returns A `FigletResult` containing the `HorizontalLayout`, or a list of
 * `FigletException`s accumulated during parsing.
 */


function fromHeader(header) {
  return E.map_(E.tuple(fromOldLayout(header), fromFullLayout(header)), ([oldHorizontal, fullHorizontal]) => O.getOrElse_(fullHorizontal, () => oldHorizontal));
}
/**
 * Interprets the `fullLayout` header settings and returns the selected
 * `HorizontalLayout`.
 *
 * @param header The `FigHeader` where the `HorizontalLayout` is defined.
 * @returns A `FigletResult` containing the `HorizontalLayout`, or a list of
 * `FigletException`s accumulated during parsing.
 */


function fromFullLayout(header) {
  return O.fold_(header.fullLayout, () => E.right(O.emptyOf()), settings => {
    if (!hasFullLayout(settings, FullLayout.HorizontalFitting) && !hasFullLayout(settings, FullLayout.HorizontalSmushing)) {
      return E.right(O.some(FullWidth));
    }

    if (hasFullLayout(settings, FullLayout.HorizontalFitting) && !hasFullLayout(settings, FullLayout.HorizontalSmushing)) {
      return E.right(O.some(HorizontalFitting));
    }

    const selectedSmushingRules = A.intersection_(FullLayout.equalFullLayout)(C.toArray(settings), C.toArray(FullLayout.horizontalSmushingRules));

    if (hasFullLayout(settings, FullLayout.HorizontalSmushing) && !A.isEmpty(selectedSmushingRules)) {
      return E.map_(HSR.fromFullLayout(header), rules => O.some(ControlledSmushing(rules)));
    }

    return E.right(O.some(UniversalSmushing));
  });
}
/**
 * Interprets the `oldLayout` header settings and returns the selected
 * `HorizontalLayout`.
 *
 * @param header The `FigHeader` where the `HorizontalLayout` is defined.
 * @returns A `FigletResult` containing the `HorizontalLayout`, or a list of
 * `FigletException`s accumulated during parsing.
 */


function fromOldLayout(header) {
  const settings = C.toArray(header.oldLayout);
  const eqLayoutArray = A.getEqual(OldLayout.equalOldLayout);

  if (eqLayoutArray.equals(settings, A.single(OldLayout.FullWidth))) {
    return E.right(FullWidth);
  }

  if (eqLayoutArray.equals(settings, A.single(OldLayout.HorizontalFitting))) {
    return E.right(HorizontalFitting);
  }

  const hasOldLayout = A.elem_(OldLayout.equalOldLayout);

  if (!hasOldLayout(settings, OldLayout.FullWidth) && !hasOldLayout(settings, OldLayout.HorizontalFitting)) {
    return E.map_(HSR.fromOldLayout(header), ControlledSmushing);
  }

  return E.left(NA.single(new _index3.FigFontError({
    message: "Could not convert layout settings found in header to a " + `HorizontalLayout, received "${settings.map(({
      value
    }) => value).join(", ")}"`
  })));
} // -----------------------------------------------------------------------------
// Instances
// -----------------------------------------------------------------------------


const equalHorizontalLayout = /*#__PURE__*/Eq.makeEqual(Structural.equals); // -----------------------------------------------------------------------------
// Utilities
// -----------------------------------------------------------------------------

exports.equalHorizontalLayout = equalHorizontalLayout;

function hasFullLayout(settings, layout) {
  return C.elem_(settings, FullLayout.equalFullLayout, layout);
}
//# sourceMappingURL=index.js.map