import { WireType } from "@protobuf-ts/runtime";
import { UnknownFieldHandler } from "@protobuf-ts/runtime";
import { reflectionMergePartial } from "@protobuf-ts/runtime";
import { typeofJsonValue } from "@protobuf-ts/runtime";
import { lowerCamelCase } from "@protobuf-ts/runtime";
import { MessageType } from "@protobuf-ts/runtime";
class FieldMask$Type extends MessageType {
  constructor() {
    super("google.protobuf.FieldMask", [
      {
        no: 1,
        name: "paths",
        kind: "scalar",
        repeat: 2,
        T: 9
      }
    ]);
  }
  /**
   * Encode `FieldMask` to JSON object.
   */
  internalJsonWrite(message, options) {
    const invalidFieldMaskJsonRegex = /[A-Z]|(_([.0-9_]|$))/g;
    return message.paths.map((p) => {
      if (invalidFieldMaskJsonRegex.test(p))
        throw new Error(
          'Unable to encode FieldMask to JSON. lowerCamelCase of path name "' + p + '" is irreversible.'
        );
      return lowerCamelCase(p);
    }).join(",");
  }
  /**
   * Decode `FieldMask` from JSON object.
   */
  internalJsonRead(json, options, target) {
    if (typeof json !== "string")
      throw new Error(
        "Unable to parse FieldMask from JSON " + typeofJsonValue(json) + ". Expected string."
      );
    if (!target) target = this.create();
    if (json === "") return target;
    let camelToSnake = (str) => {
      if (str.includes("_"))
        throw new Error(
          "Unable to parse FieldMask from JSON. Path names must be lowerCamelCase."
        );
      let sc = str.replace(
        /[A-Z]/g,
        (letter) => "_" + letter.toLowerCase()
      );
      return sc;
    };
    target.paths = json.split(",").map(camelToSnake);
    return target;
  }
  create(value) {
    const message = globalThis.Object.create(this.messagePrototype);
    message.paths = [];
    if (value !== void 0)
      reflectionMergePartial(this, message, value);
    return message;
  }
  internalBinaryRead(reader, length, options, target) {
    let message = target ?? this.create(), end = reader.pos + length;
    while (reader.pos < end) {
      let [fieldNo, wireType] = reader.tag();
      switch (fieldNo) {
        case /* repeated string paths */
        1:
          message.paths.push(reader.string());
          break;
        default:
          let u = options.readUnknownField;
          if (u === "throw")
            throw new globalThis.Error(
              `Unknown field ${fieldNo} (wire type ${wireType}) for ${this.typeName}`
            );
          let d = reader.skip(wireType);
          if (u !== false)
            (u === true ? UnknownFieldHandler.onRead : u)(
              this.typeName,
              message,
              fieldNo,
              wireType,
              d
            );
      }
    }
    return message;
  }
  internalBinaryWrite(message, writer, options) {
    for (let i = 0; i < message.paths.length; i++)
      writer.tag(1, WireType.LengthDelimited).string(message.paths[i]);
    let u = options.writeUnknownFields;
    if (u !== false)
      (u == true ? UnknownFieldHandler.onWrite : u)(
        this.typeName,
        message,
        writer
      );
    return writer;
  }
}
const FieldMask = new FieldMask$Type();
export {
  FieldMask
};
//# sourceMappingURL=field_mask.js.map