// ets_tracing: off
import * as A from "@effect-ts/core/Collections/Immutable/Array";
import * as C from "@effect-ts/core/Collections/Immutable/Chunk";
import * as NA from "@effect-ts/core/Collections/Immutable/NonEmptyArray";
import * as E from "@effect-ts/core/Either";
import * as Eq from "@effect-ts/core/Equal";
import { pipe } from "@effect-ts/core/Function";
import * as O from "@effect-ts/core/Option";
import * as Structural from "@effect-ts/core/Structural";
import * as FullLayout from "../../FigHeader/FullLayout/index.mjs";
import * as OldLayout from "../../FigHeader/OldLayout/index.mjs";
import { FigFontError } from "../../FigletException/index.mjs";
import * as HSR from "../HorizontalSmushingRule/index.mjs"; // -----------------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------------

export const FullWidth = {
  _tag: "FullWidth"
};
export const HorizontalFitting = {
  _tag: "HorizontalFitting"
};
export const UniversalSmushing = {
  _tag: "UniversalSmushing"
};
export 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.
 */

export 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.
 */

export 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.
 */

export 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 FigFontError({
    message: "Could not convert layout settings found in header to a " + `HorizontalLayout, received "${settings.map(({
      value
    }) => value).join(", ")}"`
  })));
} // -----------------------------------------------------------------------------
// Instances
// -----------------------------------------------------------------------------

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

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