// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
//   protoc-gen-ts_proto  v2.6.1
//   protoc               v3.19.1
// source: workflows/v1-admin.proto

/* eslint-disable */
import { BinaryReader, BinaryWriter } from '@bufbuild/protobuf/wire';
import { type CallContext, type CallOptions } from 'nice-grpc-common';
import { Timestamp } from '../google/protobuf/timestamp';

export const protobufPackage = '';

export interface CancelTasksRequest {
  tasks: TaskIdRetryCount[];
  filter?: TasksFilter | undefined;
}

export interface ReplayTasksRequest {
  tasks: TaskIdRetryCount[];
  filter?: TasksFilter | undefined;
}

export interface TasksFilter {
  statuses: string[];
  since: Date | undefined;
  until?: Date | undefined;
  workflowIds: string[];
  additionalMetadata: string[];
}

export interface TaskIdRetryCount {
  taskId: number;
  retryCount: number;
}

export interface CancelTasksResponse {
  cancelledTasks: TaskIdRetryCount[];
}

export interface ReplayTasksResponse {
  replayedTasks: TaskIdRetryCount[];
}

function createBaseCancelTasksRequest(): CancelTasksRequest {
  return { tasks: [], filter: undefined };
}

export const CancelTasksRequest: MessageFns<CancelTasksRequest> = {
  encode(message: CancelTasksRequest, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.tasks) {
      TaskIdRetryCount.encode(v!, writer.uint32(10).fork()).join();
    }
    if (message.filter !== undefined) {
      TasksFilter.encode(message.filter, writer.uint32(18).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): CancelTasksRequest {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseCancelTasksRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.tasks.push(TaskIdRetryCount.decode(reader, reader.uint32()));
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.filter = TasksFilter.decode(reader, reader.uint32());
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): CancelTasksRequest {
    return {
      tasks: globalThis.Array.isArray(object?.tasks)
        ? object.tasks.map((e: any) => TaskIdRetryCount.fromJSON(e))
        : [],
      filter: isSet(object.filter) ? TasksFilter.fromJSON(object.filter) : undefined,
    };
  },

  toJSON(message: CancelTasksRequest): unknown {
    const obj: any = {};
    if (message.tasks?.length) {
      obj.tasks = message.tasks.map((e) => TaskIdRetryCount.toJSON(e));
    }
    if (message.filter !== undefined) {
      obj.filter = TasksFilter.toJSON(message.filter);
    }
    return obj;
  },

  create(base?: DeepPartial<CancelTasksRequest>): CancelTasksRequest {
    return CancelTasksRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<CancelTasksRequest>): CancelTasksRequest {
    const message = createBaseCancelTasksRequest();
    message.tasks = object.tasks?.map((e) => TaskIdRetryCount.fromPartial(e)) || [];
    message.filter =
      object.filter !== undefined && object.filter !== null
        ? TasksFilter.fromPartial(object.filter)
        : undefined;
    return message;
  },
};

function createBaseReplayTasksRequest(): ReplayTasksRequest {
  return { tasks: [], filter: undefined };
}

export const ReplayTasksRequest: MessageFns<ReplayTasksRequest> = {
  encode(message: ReplayTasksRequest, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.tasks) {
      TaskIdRetryCount.encode(v!, writer.uint32(10).fork()).join();
    }
    if (message.filter !== undefined) {
      TasksFilter.encode(message.filter, writer.uint32(18).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): ReplayTasksRequest {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseReplayTasksRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.tasks.push(TaskIdRetryCount.decode(reader, reader.uint32()));
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.filter = TasksFilter.decode(reader, reader.uint32());
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): ReplayTasksRequest {
    return {
      tasks: globalThis.Array.isArray(object?.tasks)
        ? object.tasks.map((e: any) => TaskIdRetryCount.fromJSON(e))
        : [],
      filter: isSet(object.filter) ? TasksFilter.fromJSON(object.filter) : undefined,
    };
  },

  toJSON(message: ReplayTasksRequest): unknown {
    const obj: any = {};
    if (message.tasks?.length) {
      obj.tasks = message.tasks.map((e) => TaskIdRetryCount.toJSON(e));
    }
    if (message.filter !== undefined) {
      obj.filter = TasksFilter.toJSON(message.filter);
    }
    return obj;
  },

  create(base?: DeepPartial<ReplayTasksRequest>): ReplayTasksRequest {
    return ReplayTasksRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<ReplayTasksRequest>): ReplayTasksRequest {
    const message = createBaseReplayTasksRequest();
    message.tasks = object.tasks?.map((e) => TaskIdRetryCount.fromPartial(e)) || [];
    message.filter =
      object.filter !== undefined && object.filter !== null
        ? TasksFilter.fromPartial(object.filter)
        : undefined;
    return message;
  },
};

function createBaseTasksFilter(): TasksFilter {
  return {
    statuses: [],
    since: undefined,
    until: undefined,
    workflowIds: [],
    additionalMetadata: [],
  };
}

export const TasksFilter: MessageFns<TasksFilter> = {
  encode(message: TasksFilter, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.statuses) {
      writer.uint32(10).string(v!);
    }
    if (message.since !== undefined) {
      Timestamp.encode(toTimestamp(message.since), writer.uint32(18).fork()).join();
    }
    if (message.until !== undefined) {
      Timestamp.encode(toTimestamp(message.until), writer.uint32(26).fork()).join();
    }
    for (const v of message.workflowIds) {
      writer.uint32(34).string(v!);
    }
    for (const v of message.additionalMetadata) {
      writer.uint32(42).string(v!);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): TasksFilter {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseTasksFilter();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.statuses.push(reader.string());
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.since = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          message.until = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
          continue;
        }
        case 4: {
          if (tag !== 34) {
            break;
          }

          message.workflowIds.push(reader.string());
          continue;
        }
        case 5: {
          if (tag !== 42) {
            break;
          }

          message.additionalMetadata.push(reader.string());
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): TasksFilter {
    return {
      statuses: globalThis.Array.isArray(object?.statuses)
        ? object.statuses.map((e: any) => globalThis.String(e))
        : [],
      since: isSet(object.since) ? fromJsonTimestamp(object.since) : undefined,
      until: isSet(object.until) ? fromJsonTimestamp(object.until) : undefined,
      workflowIds: globalThis.Array.isArray(object?.workflowIds)
        ? object.workflowIds.map((e: any) => globalThis.String(e))
        : [],
      additionalMetadata: globalThis.Array.isArray(object?.additionalMetadata)
        ? object.additionalMetadata.map((e: any) => globalThis.String(e))
        : [],
    };
  },

  toJSON(message: TasksFilter): unknown {
    const obj: any = {};
    if (message.statuses?.length) {
      obj.statuses = message.statuses;
    }
    if (message.since !== undefined) {
      obj.since = message.since.toISOString();
    }
    if (message.until !== undefined) {
      obj.until = message.until.toISOString();
    }
    if (message.workflowIds?.length) {
      obj.workflowIds = message.workflowIds;
    }
    if (message.additionalMetadata?.length) {
      obj.additionalMetadata = message.additionalMetadata;
    }
    return obj;
  },

  create(base?: DeepPartial<TasksFilter>): TasksFilter {
    return TasksFilter.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<TasksFilter>): TasksFilter {
    const message = createBaseTasksFilter();
    message.statuses = object.statuses?.map((e) => e) || [];
    message.since = object.since ?? undefined;
    message.until = object.until ?? undefined;
    message.workflowIds = object.workflowIds?.map((e) => e) || [];
    message.additionalMetadata = object.additionalMetadata?.map((e) => e) || [];
    return message;
  },
};

function createBaseTaskIdRetryCount(): TaskIdRetryCount {
  return { taskId: 0, retryCount: 0 };
}

export const TaskIdRetryCount: MessageFns<TaskIdRetryCount> = {
  encode(message: TaskIdRetryCount, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.taskId !== 0) {
      writer.uint32(8).int64(message.taskId);
    }
    if (message.retryCount !== 0) {
      writer.uint32(16).int32(message.retryCount);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): TaskIdRetryCount {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseTaskIdRetryCount();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 8) {
            break;
          }

          message.taskId = longToNumber(reader.int64());
          continue;
        }
        case 2: {
          if (tag !== 16) {
            break;
          }

          message.retryCount = reader.int32();
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): TaskIdRetryCount {
    return {
      taskId: isSet(object.taskId) ? globalThis.Number(object.taskId) : 0,
      retryCount: isSet(object.retryCount) ? globalThis.Number(object.retryCount) : 0,
    };
  },

  toJSON(message: TaskIdRetryCount): unknown {
    const obj: any = {};
    if (message.taskId !== 0) {
      obj.taskId = Math.round(message.taskId);
    }
    if (message.retryCount !== 0) {
      obj.retryCount = Math.round(message.retryCount);
    }
    return obj;
  },

  create(base?: DeepPartial<TaskIdRetryCount>): TaskIdRetryCount {
    return TaskIdRetryCount.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<TaskIdRetryCount>): TaskIdRetryCount {
    const message = createBaseTaskIdRetryCount();
    message.taskId = object.taskId ?? 0;
    message.retryCount = object.retryCount ?? 0;
    return message;
  },
};

function createBaseCancelTasksResponse(): CancelTasksResponse {
  return { cancelledTasks: [] };
}

export const CancelTasksResponse: MessageFns<CancelTasksResponse> = {
  encode(message: CancelTasksResponse, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.cancelledTasks) {
      TaskIdRetryCount.encode(v!, writer.uint32(10).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): CancelTasksResponse {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseCancelTasksResponse();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.cancelledTasks.push(TaskIdRetryCount.decode(reader, reader.uint32()));
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): CancelTasksResponse {
    return {
      cancelledTasks: globalThis.Array.isArray(object?.cancelledTasks)
        ? object.cancelledTasks.map((e: any) => TaskIdRetryCount.fromJSON(e))
        : [],
    };
  },

  toJSON(message: CancelTasksResponse): unknown {
    const obj: any = {};
    if (message.cancelledTasks?.length) {
      obj.cancelledTasks = message.cancelledTasks.map((e) => TaskIdRetryCount.toJSON(e));
    }
    return obj;
  },

  create(base?: DeepPartial<CancelTasksResponse>): CancelTasksResponse {
    return CancelTasksResponse.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<CancelTasksResponse>): CancelTasksResponse {
    const message = createBaseCancelTasksResponse();
    message.cancelledTasks =
      object.cancelledTasks?.map((e) => TaskIdRetryCount.fromPartial(e)) || [];
    return message;
  },
};

function createBaseReplayTasksResponse(): ReplayTasksResponse {
  return { replayedTasks: [] };
}

export const ReplayTasksResponse: MessageFns<ReplayTasksResponse> = {
  encode(message: ReplayTasksResponse, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.replayedTasks) {
      TaskIdRetryCount.encode(v!, writer.uint32(10).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): ReplayTasksResponse {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseReplayTasksResponse();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.replayedTasks.push(TaskIdRetryCount.decode(reader, reader.uint32()));
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): ReplayTasksResponse {
    return {
      replayedTasks: globalThis.Array.isArray(object?.replayedTasks)
        ? object.replayedTasks.map((e: any) => TaskIdRetryCount.fromJSON(e))
        : [],
    };
  },

  toJSON(message: ReplayTasksResponse): unknown {
    const obj: any = {};
    if (message.replayedTasks?.length) {
      obj.replayedTasks = message.replayedTasks.map((e) => TaskIdRetryCount.toJSON(e));
    }
    return obj;
  },

  create(base?: DeepPartial<ReplayTasksResponse>): ReplayTasksResponse {
    return ReplayTasksResponse.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<ReplayTasksResponse>): ReplayTasksResponse {
    const message = createBaseReplayTasksResponse();
    message.replayedTasks = object.replayedTasks?.map((e) => TaskIdRetryCount.fromPartial(e)) || [];
    return message;
  },
};

/** AdminService represents a set of RPCs for admin management of tasks, workflows, etc. */
export type AdminServiceDefinition = typeof AdminServiceDefinition;
export const AdminServiceDefinition = {
  name: 'AdminService',
  fullName: 'AdminService',
  methods: {
    cancelTasks: {
      name: 'CancelTasks',
      requestType: CancelTasksRequest,
      requestStream: false,
      responseType: CancelTasksResponse,
      responseStream: false,
      options: {},
    },
    replayTasks: {
      name: 'ReplayTasks',
      requestType: ReplayTasksRequest,
      requestStream: false,
      responseType: ReplayTasksResponse,
      responseStream: false,
      options: {},
    },
  },
} as const;

export interface AdminServiceImplementation<CallContextExt = {}> {
  cancelTasks(
    request: CancelTasksRequest,
    context: CallContext & CallContextExt
  ): Promise<DeepPartial<CancelTasksResponse>>;
  replayTasks(
    request: ReplayTasksRequest,
    context: CallContext & CallContextExt
  ): Promise<DeepPartial<ReplayTasksResponse>>;
}

export interface AdminServiceClient<CallOptionsExt = {}> {
  cancelTasks(
    request: DeepPartial<CancelTasksRequest>,
    options?: CallOptions & CallOptionsExt
  ): Promise<CancelTasksResponse>;
  replayTasks(
    request: DeepPartial<ReplayTasksRequest>,
    options?: CallOptions & CallOptionsExt
  ): Promise<ReplayTasksResponse>;
}

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

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

function toTimestamp(date: Date): Timestamp {
  const seconds = Math.trunc(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 || 0) * 1_000;
  millis += (t.nanos || 0) / 1_000_000;
  return new globalThis.Date(millis);
}

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

function longToNumber(int64: { toString(): string }): number {
  const num = globalThis.Number(int64.toString());
  if (num > globalThis.Number.MAX_SAFE_INTEGER) {
    throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER');
  }
  if (num < globalThis.Number.MIN_SAFE_INTEGER) {
    throw new globalThis.Error('Value is smaller than Number.MIN_SAFE_INTEGER');
  }
  return num;
}

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

export interface MessageFns<T> {
  encode(message: T, writer?: BinaryWriter): BinaryWriter;
  decode(input: BinaryReader | Uint8Array, length?: number): T;
  fromJSON(object: any): T;
  toJSON(message: T): unknown;
  create(base?: DeepPartial<T>): T;
  fromPartial(object: DeepPartial<T>): T;
}
