var v = Object.defineProperty;
var b = (s, t) => v(s, "name", { value: t, configurable: !0 });
import { setEnumType as N } from "./enum.js";
import { scalarEquals as d } from "./scalars.js";
import { ScalarType as m } from "../scalar.js";
import { isMessage as g } from "../is-message.js";
function j() {
  return {
    setEnumType: N,
    initPartial(s, t) {
      if (s === void 0)
        return;
      const f = t.getType();
      for (const r of f.fields.byMember()) {
        const e = r.localName, n = t, a = s;
        if (a[e] != null)
          switch (r.kind) {
            case "oneof":
              const c = a[e].case;
              if (c === void 0)
                continue;
              const l = r.findField(c);
              let u = a[e].value;
              l && l.kind == "message" && !g(u, l.T) ? u = new l.T(u) : l && l.kind === "scalar" && l.T === m.BYTES && (u = T(u)), n[e] = { case: c, value: u };
              break;
            case "scalar":
            case "enum":
              let o = a[e];
              r.T === m.BYTES && (o = r.repeated ? o.map(T) : T(o)), n[e] = o;
              break;
            case "map":
              switch (r.V.kind) {
                case "scalar":
                case "enum":
                  if (r.V.T === m.BYTES)
                    for (const [k, p] of Object.entries(a[e]))
                      n[e][k] = T(p);
                  else
                    Object.assign(n[e], a[e]);
                  break;
                case "message":
                  const i = r.V.T;
                  for (const k of Object.keys(a[e])) {
                    let p = a[e][k];
                    i.fieldWrapper || (p = new i(p)), n[e][k] = p;
                  }
                  break;
              }
              break;
            case "message":
              const y = r.T;
              if (r.repeated)
                n[e] = a[e].map((i) => g(i, y) ? i : new y(i));
              else {
                const i = a[e];
                y.fieldWrapper ? /* We can't use BytesValue.typeName as that will create a circular import */ y.typeName === "google.protobuf.BytesValue" ? n[e] = T(i) : n[e] = i : n[e] = g(i, y) ? i : new y(i);
              }
              break;
          }
      }
    },
    // TODO use isFieldSet() here to support future field presence
    equals(s, t, f) {
      return t === f ? !0 : !t || !f ? !1 : s.fields.byMember().every((r) => {
        const e = t[r.localName], n = f[r.localName];
        if (r.repeated) {
          if (e.length !== n.length)
            return !1;
          switch (r.kind) {
            case "message":
              return e.every((a, c) => r.T.equals(a, n[c]));
            case "scalar":
              return e.every((a, c) => d(r.T, a, n[c]));
            case "enum":
              return e.every((a, c) => d(m.INT32, a, n[c]));
          }
          throw new Error(`repeated cannot contain ${r.kind}`);
        }
        switch (r.kind) {
          case "message":
            return r.T.equals(e, n);
          case "enum":
            return d(m.INT32, e, n);
          case "scalar":
            return d(r.T, e, n);
          case "oneof":
            if (e.case !== n.case)
              return !1;
            const a = r.findField(e.case);
            if (a === void 0)
              return !0;
            switch (a.kind) {
              case "message":
                return a.T.equals(e.value, n.value);
              case "enum":
                return d(m.INT32, e.value, n.value);
              case "scalar":
                return d(a.T, e.value, n.value);
            }
            throw new Error(`oneof cannot contain ${a.kind}`);
          case "map":
            const c = Object.keys(e).concat(Object.keys(n));
            switch (r.V.kind) {
              case "message":
                const l = r.V.T;
                return c.every((o) => l.equals(e[o], n[o]));
              case "enum":
                return c.every((o) => d(m.INT32, e[o], n[o]));
              case "scalar":
                const u = r.V.T;
                return c.every((o) => d(u, e[o], n[o]));
            }
            break;
        }
      });
    },
    // TODO use isFieldSet() here to support future field presence
    clone(s) {
      const t = s.getType(), f = new t(), r = f;
      for (const e of t.fields.byMember()) {
        const n = s[e.localName];
        let a;
        if (e.repeated)
          a = n.map(w);
        else if (e.kind == "map") {
          a = r[e.localName];
          for (const [c, l] of Object.entries(n))
            a[c] = w(l);
        } else e.kind == "oneof" ? a = e.findField(n.case) ? { case: n.case, value: w(n.value) } : { case: void 0 } : a = w(n);
        r[e.localName] = a;
      }
      for (const e of t.runtime.bin.listUnknownFields(s))
        t.runtime.bin.onUnknownField(r, e.no, e.wireType, e.data);
      return f;
    }
  };
}
b(j, "makeUtilCommon");
function w(s) {
  if (s === void 0)
    return s;
  if (g(s))
    return s.clone();
  if (s instanceof Uint8Array) {
    const t = new Uint8Array(s.byteLength);
    return t.set(s), t;
  }
  return s;
}
b(w, "cloneSingularField");
function T(s) {
  return s instanceof Uint8Array ? s : new Uint8Array(s);
}
b(T, "toU8Arr");
export {
  j as makeUtilCommon
};
