import * as tsplus_module_1 from "@tsplus/stdlib/data/Function";
import * as tsplus_module_2 from "@effect/core/stm/TRef/operations/unsafeGet";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/unsafeGet";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/List/exists";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/List/empty";
import * as tsplus_module_8 from "@tsplus/stdlib/collections/List/prepend";
import * as tsplus_module_9 from "@effect/core/stm/TRef/operations/unsafeSet";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/List/definition";
import * as tsplus_module_11 from "@tsplus/stdlib/collections/Chunk/size";
import * as tsplus_module_12 from "@tsplus/stdlib/data/Maybe/fromPredicate";
import * as tsplus_module_13 from "@effect/core/stm/STM/operations/effect";
import * as tsplus_module_14 from "@effect/core/stm/STM/operations/continueOrRetry";
import { concreteTArray } from "@effect/core/stm/TArray/operations/_internal/InternalTArray";
import { concreteTMap } from "@effect/core/stm/TMap/operations/_internal/InternalTMap";
/**
 * Takes all matching values, or retries until there is at least one.
 *
 * @tsplus fluent ets/TMap takeSome
 */

export function takeSome_(self, pf) {
  void 0;
  return tsplus_module_14.continueOrRetry_(tsplus_module_13.effect(journal => {
    const buckets = tsplus_module_2.unsafeGet_(self.tBuckets, journal);
    const chunkBuilder = tsplus_module_3.builder();
    void 0;
    const capacity = buckets.chunk.length;
    let i = 0;
    let newSize = 0;

    while (i < capacity) {
      const bucket = tsplus_module_2.unsafeGet_(tsplus_module_4.unsafeGet_(buckets.chunk, i), journal);
      const recreate = tsplus_module_6.exists_(bucket, _ => tsplus_module_5.isSome(pf(_)));

      if (recreate) {
        let newBucket = tsplus_module_7.empty();

        for (const pair of bucket) {
          const result = pf(pair);

          if (tsplus_module_5.isSome(result)) {
            chunkBuilder.append(result.value);
          } else {
            newBucket = tsplus_module_8.prepend_(newBucket, pair);
            newSize += 1;
          }
        }

        tsplus_module_9.unsafeSet_(tsplus_module_4.unsafeGet_(buckets.chunk, i), newBucket, journal);
      } else {
        newSize += tsplus_module_10.length(bucket);
      }

      i += 1;
    }

    tsplus_module_9.unsafeSet_(self.tSize, newSize, journal);
    return tsplus_module_12.fromPredicate(chunkBuilder.build(), _ => tsplus_module_11.size(_) > 0);
  }), tsplus_module_1.identity);
}
/**
 * Takes all matching values, or retries until there is at least one.
 *
 * @tsplus static ets/TMap/Aspects takeSome
 */

export const takeSome = pf => self => takeSome_(self, pf);
//# sourceMappingURL=takeSome.mjs.map