"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
var _a, _b, _c
Object.defineProperty(exports, "__esModule", { value: true })
exports.toArray =
  exports.prepend_ =
  exports.prepend =
  exports.append_ =
  exports.append =
  exports.concat_ =
  exports.concat =
  exports.of =
  exports.init =
  exports.IConcat =
  exports.IElement =
  exports.IEmpty =
  exports.isFreeAssociative =
    void 0
// tracing: off
require("../Operator")
const Stack_1 = require("../Stack")
const St = __importStar(require("../Structural"))
const _brand = Symbol()
function isFreeAssociative(self) {
  return typeof self === "object" && self != null && _brand in self
}
exports.isFreeAssociative = isFreeAssociative
class IEmpty {
  constructor() {
    this._tag = "Empty"
    this[_a] = _brand
  }
  get [((_a = _brand), St.hashSym)]() {
    return St.hash(toArray(this))
  }
  [St.equalsSym](that) {
    return isFreeAssociative(that) && St.equals(toArray(this), toArray(that))
  }
}
exports.IEmpty = IEmpty
class IElement {
  constructor(element) {
    this.element = element
    this._tag = "Element"
    this[_b] = _brand
  }
  get [((_b = _brand), St.hashSym)]() {
    return St.hash(toArray(this))
  }
  [St.equalsSym](that) {
    return isFreeAssociative(that) && St.equals(toArray(this), toArray(that))
  }
}
exports.IElement = IElement
class IConcat {
  constructor(left, right) {
    this.left = left
    this.right = right
    this._tag = "Concat"
    this[_c] = _brand
  }
}
exports.IConcat = IConcat
_c = _brand
function init() {
  return new IEmpty()
}
exports.init = init
function of(a) {
  return new IElement(a)
}
exports.of = of
function concat(r) {
  return (l) => new IConcat(l, r)
}
exports.concat = concat
function concat_(l, r) {
  return new IConcat(l, r)
}
exports.concat_ = concat_
function append(a) {
  return (_) => new IConcat(_, new IElement(a))
}
exports.append = append
function append_(_, a) {
  return new IConcat(_, new IElement(a))
}
exports.append_ = append_
function prepend(a) {
  return (_) => new IConcat(new IElement(a), _)
}
exports.prepend = prepend
function prepend_(_, a) {
  return new IConcat(new IElement(a), _)
}
exports.prepend_ = prepend_
function toArray(_) {
  const as = []
  let current = _
  let stack = undefined
  while (typeof current !== "undefined") {
    switch (current._tag) {
      case "Empty": {
        current = undefined
        break
      }
      case "Element": {
        as.push(current.element)
        current = undefined
        break
      }
      case "Concat": {
        const p = stack
        stack = new Stack_1.Stack(current.right, p)
        current = current.left
        break
      }
    }
    if (typeof current === "undefined") {
      if (typeof stack !== "undefined") {
        current = stack.value
        stack = stack.previous
      }
    }
  }
  return as
}
exports.toArray = toArray
//# sourceMappingURL=index.js.map
