"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.equalFigHeader = exports.FigHeader = void 0;
exports.fromLine = fromLine;
exports.toSingleLine = toSingleLine;

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

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 PrintDirection = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../FigFont/PrintDirection/index.js"));

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

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

var OldLayout = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./OldLayout/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
// -----------------------------------------------------------------------------

/**
 * A Figlet font file header that contains raw configuration settings for the
 * Figlet font.
 *
 * A `FigHeader` cannot be instantiated directly. Instead, one must use the
 * constructor definitions to perform validation of the header.
 *
 * ```text
 * An example header line might look like the following:
 *
 *         flf2a$ 6 5 20 15 3 0 143 229    NOTE: The first five characters in
 *           |  | | | |  |  | |  |   |     the entire file must be "flf2a".
 *          /  /  | | |  |  | |  |   \
 * Signature  /  /  | |  |  | |   \   Codetag_Count
 *   Hardblank  /  /  |  |  |  \   Full_Layout*
 *        Height  /   |  |   \  Print_Direction
 *        Baseline   /    \   Comment_Lines
 *         Max_Length      Old_Layout*
 * ```
 * See http://www.jave.de/docs/figfont.txt for more information.
 */
class FigHeader extends _Case.Case {} // -----------------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------------


exports.FigHeader = FigHeader;
const SIGNATURE_INDEX = 0;
const HEIGHT_INDEX = 1;
const BASELINE_INDEX = 2;
const MAXLENGTH_INDEX = 3;
const OLDLAYOUT_INDEX = 4;
const COMMENTLINES_INDEX = 5;
const PRINTDIRECTION_INDEX = 6;
const FULLLAYOUT_INDEX = 7;
const CODETAGCOUNT_INDEX = 8;
/**
 * Creates a new `FigHeader` from a the string representing the Figlet font
 * header.
 *
 * @param line The string containing the full header of the Figlet font file.
 * @return A `FigletResult` containing the new `FigHeader`, or a list of the
 * errors that occurred during parsing of the header line.
 */

function fromLine(line) {
  const splitLine = C.from(line.trim().split(" "));

  if (splitLine.length < 6) {
    return E.left(NA.single(new _index2.FigHeaderError({
      message: `Wrong number of parameters in the Figlet font header. ` + `Expected at least 6 parameters but found ${splitLine.length}.`
    })));
  }

  const [signatureText, hardblankText] = C.splitAt_(C.from(C.unsafeGet_(splitLine, SIGNATURE_INDEX).split("")), 5);
  return E.map_(E.struct({
    signature: validateSignature(C.join_(signatureText, "")),
    hardblank: validateHardblank(C.join_(hardblankText, "")),
    height: validateHeight(C.unsafeGet_(splitLine, HEIGHT_INDEX)),
    baseline: validateBaseline(C.unsafeGet_(splitLine, BASELINE_INDEX)),
    maxLength: validateMaxLength(C.unsafeGet_(splitLine, MAXLENGTH_INDEX)),
    oldLayout: validateOldLayout(C.unsafeGet_(splitLine, OLDLAYOUT_INDEX)),
    commentLines: validateCommentLines(C.unsafeGet_(splitLine, COMMENTLINES_INDEX)),
    printDirection: validatePrintDirection(C.get_(splitLine, PRINTDIRECTION_INDEX)),
    fullLayout: validateFullLayout(C.get_(splitLine, FULLLAYOUT_INDEX)),
    codeTagCount: validateCodetagCount(C.get_(splitLine, CODETAGCOUNT_INDEX))
  }), _ => new FigHeader(_));
} // -----------------------------------------------------------------------------
// Destructors
// -----------------------------------------------------------------------------


function toSingleLine(self) {
  const oldLayoutNum = C.reduce_(self.oldLayout, 0, (b, a) => b + a.value);
  const printDirectionNum = O.getOrElse_(O.map_(self.printDirection, x => ` ${x.value}`), () => "");
  const fullLayoutNum = O.getOrElse_(O.map_(self.fullLayout, x => ` ${C.reduce_(x, 0, (b, a) => b + a.value)}`), () => "");
  const codetagCountNum = O.getOrElse_(O.map_(self.codeTagCount, x => ` ${x}`), () => "");
  return `${self.signature}${self.hardblank} ` + `${self.height} ${self.baseline} ${self.maxLength} ${oldLayoutNum} ` + `${self.commentLines}${printDirectionNum}${fullLayoutNum}${codetagCountNum}`;
} // -----------------------------------------------------------------------------
// Validation
// -----------------------------------------------------------------------------


function headerFieldToInteger(s, field) {
  const n = Number.parseFloat(s);
  return Number.isNaN(n) || !Number.isInteger(n) ? E.left(NA.single(new _index2.FigHeaderError({
    message: `Unable to parse header field "${field}" to integer, received: "${s}"`
  }))) : E.right(n);
}

function headerFieldToPositiveInteger(s, field) {
  return E.chain_(headerFieldToInteger(s, field), E.fromPredicate(n => n > 0, n => NA.single(new _index2.FigHeaderError({
    message: `Header field "${field}" must be positive, received: "${n}"`
  }))));
}

function headerFieldToNonNegativeInteger(s, field) {
  return E.chain_(headerFieldToInteger(s, field), E.fromPredicate(n => n >= 0, n => NA.single(new _index2.FigHeaderError({
    message: `Header field "${field}" must be non-negative, received: "${n}"`
  }))));
}

function validateSignature(signature) {
  return E.fromPredicate_(signature, s => s === "flf2a", s => NA.single(new _index2.FigHeaderError({
    message: `Incorrect Figlet font signature, received "${s}"`
  })));
}

function validateHardblank(hardblank) {
  return E.fromPredicate_(hardblank, s => s.length === 1, s => NA.single(new _index2.FigHeaderError({
    message: 'Header field "hardblank" must only be a single character, ' + `received "${s}"`
  })));
}

function validateHeight(height) {
  return headerFieldToPositiveInteger(height, "height");
}

function validateBaseline(baseline) {
  return headerFieldToPositiveInteger(baseline, "baseline");
}

function validateMaxLength(maxLength) {
  return headerFieldToPositiveInteger(maxLength, "maxLength");
}

function validateOldLayout(oldLayout) {
  return E.chain_(headerFieldToInteger(oldLayout, "oldLayout"), OldLayout.fromValue);
}

function validateCommentLines(commentLines) {
  return headerFieldToNonNegativeInteger(commentLines, "commentLines");
}

function validatePrintDirection(printDirection) {
  return O.forEachF(E.Applicative)(value => E.chain_(headerFieldToInteger(value, "printDirection"), PrintDirection.fromValue))(printDirection);
}

function validateFullLayout(fullLayout) {
  return O.forEachF(E.Applicative)(value => E.chain_(headerFieldToInteger(value, "fullLayout"), FullLayout.fromValue))(fullLayout);
}

function validateCodetagCount(codetagCount) {
  return O.forEachF(E.Applicative)(value => headerFieldToNonNegativeInteger(value, "codetagCount"))(codetagCount);
} // -----------------------------------------------------------------------------
// Instances
// -----------------------------------------------------------------------------


const equalFigHeader = /*#__PURE__*/Eq.makeEqual(Structural.equals);
exports.equalFigHeader = equalFigHeader;
//# sourceMappingURL=index.js.map