/* eslint-disable */
import _m0 from "protobufjs/minimal.js";
import { Timestamp } from "../google/protobuf/timestamp.js";

export const protobufPackage = "mapeo";

export interface Common_1 {
  created_at?: Date;
  deviceId?:
    | string
    | undefined;
  /** 32-byte random generated number */
  id: Buffer;
  links: string[];
  timestamp?: Date | undefined;
  userId?: string | undefined;
}

function createBaseCommon_1(): Common_1 {
  return {
    created_at: undefined,
    deviceId: undefined,
    id: Buffer.alloc(0),
    links: [],
    timestamp: undefined,
    userId: undefined,
  };
}

export const Common_1 = {
  encode(message: Common_1, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.created_at !== undefined) {
      Timestamp.encode(toTimestamp(message.created_at), writer.uint32(10).fork()).ldelim();
    }
    if (message.deviceId !== undefined) {
      writer.uint32(18).string(message.deviceId);
    }
    if (message.id.length !== 0) {
      writer.uint32(26).bytes(message.id);
    }
    for (const v of message.links) {
      writer.uint32(34).string(v!);
    }
    if (message.timestamp !== undefined) {
      Timestamp.encode(toTimestamp(message.timestamp), writer.uint32(42).fork()).ldelim();
    }
    if (message.userId !== undefined) {
      writer.uint32(50).string(message.userId);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): Common_1 {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseCommon_1();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.created_at = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
          break;
        case 2:
          message.deviceId = reader.string();
          break;
        case 3:
          message.id = reader.bytes() as Buffer;
          break;
        case 4:
          message.links.push(reader.string());
          break;
        case 5:
          message.timestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
          break;
        case 6:
          message.userId = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },

  fromJSON(object: any): Common_1 {
    return {
      created_at: isSet(object.created_at) ? fromJsonTimestamp(object.created_at) : undefined,
      deviceId: isSet(object.deviceId) ? String(object.deviceId) : undefined,
      id: isSet(object.id) ? Buffer.from(bytesFromBase64(object.id)) : Buffer.alloc(0),
      links: Array.isArray(object?.links) ? object.links.map((e: any) => String(e)) : [],
      timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined,
      userId: isSet(object.userId) ? String(object.userId) : undefined,
    };
  },

  toJSON(message: Common_1): unknown {
    const obj: any = {};
    message.created_at !== undefined && (obj.created_at = message.created_at.toISOString());
    message.deviceId !== undefined && (obj.deviceId = message.deviceId);
    message.id !== undefined && (obj.id = base64FromBytes(message.id !== undefined ? message.id : Buffer.alloc(0)));
    if (message.links) {
      obj.links = message.links.map((e) => e);
    } else {
      obj.links = [];
    }
    message.timestamp !== undefined && (obj.timestamp = message.timestamp.toISOString());
    message.userId !== undefined && (obj.userId = message.userId);
    return obj;
  },

  create<I extends Exact<DeepPartial<Common_1>, I>>(base?: I): Common_1 {
    return Common_1.fromPartial(base ?? {});
  },

  fromPartial<I extends Exact<DeepPartial<Common_1>, I>>(object: I): Common_1 {
    const message = createBaseCommon_1();
    message.created_at = object.created_at ?? undefined;
    message.deviceId = object.deviceId ?? undefined;
    message.id = object.id ?? Buffer.alloc(0);
    message.links = object.links?.map((e) => e) || [];
    message.timestamp = object.timestamp ?? undefined;
    message.userId = object.userId ?? undefined;
    return message;
  },
};

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var tsProtoGlobalThis: any = (() => {
  if (typeof globalThis !== "undefined") {
    return globalThis;
  }
  if (typeof self !== "undefined") {
    return self;
  }
  if (typeof window !== "undefined") {
    return window;
  }
  if (typeof global !== "undefined") {
    return global;
  }
  throw "Unable to locate global object";
})();

function bytesFromBase64(b64: string): Uint8Array {
  if (tsProtoGlobalThis.Buffer) {
    return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
  } else {
    const bin = tsProtoGlobalThis.atob(b64);
    const arr = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; ++i) {
      arr[i] = bin.charCodeAt(i);
    }
    return arr;
  }
}

function base64FromBytes(arr: Uint8Array): string {
  if (tsProtoGlobalThis.Buffer) {
    return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
  } else {
    const bin: string[] = [];
    arr.forEach((byte) => {
      bin.push(String.fromCharCode(byte));
    });
    return tsProtoGlobalThis.btoa(bin.join(""));
  }
}

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
  : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
  : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
  : Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
  : P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function toTimestamp(date: Date): Timestamp {
  const seconds = date.getTime() / 1_000;
  const nanos = (date.getTime() % 1_000) * 1_000_000;
  return { seconds, nanos };
}

function fromTimestamp(t: Timestamp): Date {
  let millis = t.seconds * 1_000;
  millis += t.nanos / 1_000_000;
  return new Date(millis);
}

function fromJsonTimestamp(o: any): Date {
  if (o instanceof Date) {
    return o;
  } else if (typeof o === "string") {
    return new Date(o);
  } else {
    return fromTimestamp(Timestamp.fromJSON(o));
  }
}

function isSet(value: any): boolean {
  return value !== null && value !== undefined;
}
