"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
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.findLast =
  exports.findLast_ =
  exports.find =
  exports.find_ =
  exports.get =
  exports.get_ =
  exports.empty =
  exports.make =
  exports.fromIterable =
  exports.TArray =
  exports.TArrayTypeId =
    void 0
const tracing_1 = __importStar(require("../../Tracing"))
const fileName_1 = "(@effect-ts/system): _src/Transactional/TArray/index.ts"
const GlobalExceptions_1 = require("../../GlobalExceptions")
const O = __importStar(require("../../Option"))
const STM = __importStar(require("../STM"))
const TRef = __importStar(require("../TRef"))
exports.TArrayTypeId = Symbol()
class TArray {
  constructor(array) {
    this.array = array
    this._typeId = exports.TArrayTypeId
  }
}
exports.TArray = TArray
/**
 * Makes a new `TArray` initialized with provided iterable.
 */
function fromIterable(it) {
  return STM.map_(STM.forEach_(it, TRef.make), (as) => new TArray(as))
}
exports.fromIterable = fromIterable
/**
 * Makes a new `TArray` that is initialized with specified values.
 */
function make(...data) {
  return fromIterable(data)
}
exports.make = make
/**
 * Makes a new `TArray` that is initialized with specified values.
 */
function empty() {
  return fromIterable([])
}
exports.empty = empty
/**
 * Extracts value from ref in array.
 */
function get_(self, index) {
  if (!Number.isInteger(index) || index < 0 || index >= self.array.length) {
    return STM.die(new GlobalExceptions_1.ArrayIndexOutOfBoundsException(index))
  }
  return TRef.get(self.array[index])
}
exports.get_ = get_
/**
 * Extracts value from ref in array.
 *
 * @dataFirst get_
 */
function get(index) {
  return (self) => get_(self, index)
}
exports.get = get
/**
 * Find the first element in the array matching a predicate.
 */
function find_(self, p) {
  return new STM.STMEffect((journal) => {
    let i = 0
    while (i < self.array.length) {
      const a = TRef.unsafeGet_(self.array[i], journal)
      if (p(a)) {
        return O.some(a)
      }
      i++
    }
    return O.none
  })
}
exports.find_ = find_
/**
 * Find the first element in the array matching a predicate.
 *
 * @dataFirst find_
 */
function find(p) {
  return (self) => find_(self, p)
}
exports.find = find
/**
 * Find the last element in the array matching a predicate.
 */
function findLast_(self, p) {
  return new STM.STMEffect((journal) => {
    let i = 0
    let res = O.emptyOf()
    while (i < self.array.length) {
      const a = TRef.unsafeGet_(self.array[i], journal)
      if (p(a)) {
        res = O.some(a)
      }
      i++
    }
    return res
  })
}
exports.findLast_ = findLast_
/**
 * Find the last element in the array matching a predicate.
 *
 * @dataFirst find_
 */
function findLast(p) {
  return (self) => findLast_(self, p)
}
exports.findLast = findLast
//# sourceMappingURL=index.js.map
