// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Mocha from "mocha";
import * as Belt_List from "@rescript/runtime/lib/es6/Belt_List.mjs";
import * as Pervasives from "@rescript/runtime/lib/es6/Pervasives.mjs";
import * as Test_utils from "./test_utils.mjs";
import * as Primitive_int from "@rescript/runtime/lib/es6/Primitive_int.mjs";

function height(x) {
  if (typeof x !== "object") {
    return 0;
  } else {
    return x._4;
  }
}

function create(l, x, d, r) {
  let hl = height(l);
  let hr = height(r);
  return {
    TAG: "Node",
    _0: l,
    _1: x,
    _2: d,
    _3: r,
    _4: hl >= hr ? hl + 1 | 0 : hr + 1 | 0
  };
}

function bal(l, x, d, r) {
  let hl;
  hl = typeof l !== "object" ? 0 : l._4;
  let hr;
  hr = typeof r !== "object" ? 0 : r._4;
  if (hl > (hr + 2 | 0)) {
    if (typeof l !== "object") {
      return Pervasives.invalid_arg("Map.bal");
    }
    let lr = l._3;
    let ld = l._2;
    let lv = l._1;
    let ll = l._0;
    if (height(ll) >= height(lr)) {
      return create(ll, lv, ld, create(lr, x, d, r));
    } else if (typeof lr !== "object") {
      return Pervasives.invalid_arg("Map.bal");
    } else {
      return create(create(ll, lv, ld, lr._0), lr._1, lr._2, create(lr._3, x, d, r));
    }
  }
  if (hr <= (hl + 2 | 0)) {
    return {
      TAG: "Node",
      _0: l,
      _1: x,
      _2: d,
      _3: r,
      _4: hl >= hr ? hl + 1 | 0 : hr + 1 | 0
    };
  }
  if (typeof r !== "object") {
    return Pervasives.invalid_arg("Map.bal");
  }
  let rr = r._3;
  let rd = r._2;
  let rv = r._1;
  let rl = r._0;
  if (height(rr) >= height(rl)) {
    return create(create(l, x, d, rl), rv, rd, rr);
  } else if (typeof rl !== "object") {
    return Pervasives.invalid_arg("Map.bal");
  } else {
    return create(create(l, x, d, rl._0), rl._1, rl._2, create(rl._3, rv, rd, rr));
  }
}

function add(x, data, x_) {
  if (typeof x_ !== "object") {
    return {
      TAG: "Node",
      _0: "Empty",
      _1: x,
      _2: data,
      _3: "Empty",
      _4: 1
    };
  }
  let r = x_._3;
  let d = x_._2;
  let v = x_._1;
  let l = x_._0;
  let c = Primitive_int.compare(x, v);
  if (c === 0) {
    return {
      TAG: "Node",
      _0: l,
      _1: x,
      _2: data,
      _3: r,
      _4: x_._4
    };
  } else if (c < 0) {
    return bal(add(x, data, l), v, d, r);
  } else {
    return bal(l, v, d, add(x, data, r));
  }
}

function find(x, _x_) {
  while (true) {
    let x_ = _x_;
    if (typeof x_ !== "object") {
      throw {
        RE_EXN_ID: "Not_found",
        Error: new Error()
      };
    }
    let c = Primitive_int.compare(x, x_._1);
    if (c === 0) {
      return x_._2;
    }
    _x_ = c < 0 ? x_._0 : x_._3;
    continue;
  };
}

let m = Belt_List.reduceReverse({
  hd: [
    10,
    /* 'a' */97
  ],
  tl: {
    hd: [
      3,
      /* 'b' */98
    ],
    tl: {
      hd: [
        7,
        /* 'c' */99
      ],
      tl: {
        hd: [
          20,
          /* 'd' */100
        ],
        tl: /* [] */0
      }
    }
  }
}, "Empty", (acc, param) => add(param[0], param[1], acc));

Mocha.describe("Inline_map_test", () => {
  Mocha.test("inline map test", () => Test_utils.eq("File \"inline_map_test.res\", line 420, characters 7-14", find(10, m), /* 'a' */97));
});

/* m Not a pure module */
