"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.find = void 0;
exports.find_ = find_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/io/Eval/run"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/io/Eval/succeed"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/io/Eval/succeedNow"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/io/Eval/suspend"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/io/Eval/flatMap"));

var _definition = /*#__PURE__*/require("@effect/core/io/Cause/definition");

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; }

/**
 * Finds something and extracts some details from it.
 *
 * @tsplus fluent ets/Cause find
 */
function find_(self, f) {
  return tsplus_module_1.run(findSafe(self, f));
}
/**
 * Finds something and extracts some details from it.
 *
 * @tsplus static ets/Cause/Aspects find
 */


const find = f => self => find_(self, f);

exports.find = find;

function findSafe(self, f) {
  const result = f(self);

  if (result._tag === "Some") {
    return tsplus_module_2.succeed(() => result);
  }

  void 0;

  switch (self._tag) {
    case "Both":
    case "Then":
      return tsplus_module_5.flatMap_(tsplus_module_4.suspend(() => findSafe(self.left, f)), leftResult => leftResult._tag === "Some" ? tsplus_module_3.succeedNow(leftResult) : findSafe(self.right, f));

    case "Stackless":
      {
        return tsplus_module_4.suspend(() => findSafe(self.cause, f));
      }

    default:
      return tsplus_module_2.succeed(() => result);
  }
}
//# sourceMappingURL=find.js.map