// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
//   protoc-gen-ts_proto  v2.11.6
//   protoc               v3.19.1
// source: v1/shared/condition.proto

/* eslint-disable */
import { BinaryReader, BinaryWriter } from '@bufbuild/protobuf/wire';
import { Timestamp } from '../../google/protobuf/timestamp';

export const protobufPackage = 'v1';

export enum Action {
  CREATE = 0,
  QUEUE = 1,
  CANCEL = 2,
  SKIP = 3,
  UNRECOGNIZED = -1,
}

export function actionFromJSON(object: any): Action {
  switch (object) {
    case 0:
    case 'CREATE':
      return Action.CREATE;
    case 1:
    case 'QUEUE':
      return Action.QUEUE;
    case 2:
    case 'CANCEL':
      return Action.CANCEL;
    case 3:
    case 'SKIP':
      return Action.SKIP;
    case -1:
    case 'UNRECOGNIZED':
    default:
      return Action.UNRECOGNIZED;
  }
}

export function actionToJSON(object: Action): string {
  switch (object) {
    case Action.CREATE:
      return 'CREATE';
    case Action.QUEUE:
      return 'QUEUE';
    case Action.CANCEL:
      return 'CANCEL';
    case Action.SKIP:
      return 'SKIP';
    case Action.UNRECOGNIZED:
    default:
      return 'UNRECOGNIZED';
  }
}

export interface BaseMatchCondition {
  readableDataKey: string;
  action: Action;
  /** a UUID defining the OR group for this condition */
  orGroupId: string;
  expression: string;
}

export interface ParentOverrideMatchCondition {
  base: BaseMatchCondition | undefined;
  parentReadableId: string;
}

export interface SleepMatchCondition {
  base: BaseMatchCondition | undefined;
  /** a duration string indicating how long to sleep */
  sleepFor: string;
}

export interface UserEventMatchCondition {
  base: BaseMatchCondition | undefined;
  userEventKey: string;
  /** an optional scope for the user event condition (similar to scopes on event filters) */
  eventScope?: string | undefined;
  considerEventsSince?: Date | undefined;
}

export interface TaskConditions {
  parentOverrideConditions: ParentOverrideMatchCondition[];
  sleepConditions: SleepMatchCondition[];
  userEventConditions: UserEventMatchCondition[];
}

export interface DurableEventListenerConditions {
  sleepConditions: SleepMatchCondition[];
  userEventConditions: UserEventMatchCondition[];
}

function createBaseBaseMatchCondition(): BaseMatchCondition {
  return { readableDataKey: '', action: 0, orGroupId: '', expression: '' };
}

export const BaseMatchCondition: MessageFns<BaseMatchCondition> = {
  encode(message: BaseMatchCondition, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.readableDataKey !== '') {
      writer.uint32(10).string(message.readableDataKey);
    }
    if (message.action !== 0) {
      writer.uint32(16).int32(message.action);
    }
    if (message.orGroupId !== '') {
      writer.uint32(26).string(message.orGroupId);
    }
    if (message.expression !== '') {
      writer.uint32(34).string(message.expression);
    }
    return writer;
  },

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

          message.readableDataKey = reader.string();
          continue;
        }
        case 2: {
          if (tag !== 16) {
            break;
          }

          message.action = reader.int32() as any;
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          message.orGroupId = reader.string();
          continue;
        }
        case 4: {
          if (tag !== 34) {
            break;
          }

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

  fromJSON(object: any): BaseMatchCondition {
    return {
      readableDataKey: isSet(object.readableDataKey)
        ? globalThis.String(object.readableDataKey)
        : isSet(object.readable_data_key)
          ? globalThis.String(object.readable_data_key)
          : '',
      action: isSet(object.action) ? actionFromJSON(object.action) : 0,
      orGroupId: isSet(object.orGroupId)
        ? globalThis.String(object.orGroupId)
        : isSet(object.or_group_id)
          ? globalThis.String(object.or_group_id)
          : '',
      expression: isSet(object.expression) ? globalThis.String(object.expression) : '',
    };
  },

  toJSON(message: BaseMatchCondition): unknown {
    const obj: any = {};
    if (message.readableDataKey !== '') {
      obj.readableDataKey = message.readableDataKey;
    }
    if (message.action !== 0) {
      obj.action = actionToJSON(message.action);
    }
    if (message.orGroupId !== '') {
      obj.orGroupId = message.orGroupId;
    }
    if (message.expression !== '') {
      obj.expression = message.expression;
    }
    return obj;
  },

  create(base?: DeepPartial<BaseMatchCondition>): BaseMatchCondition {
    return BaseMatchCondition.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<BaseMatchCondition>): BaseMatchCondition {
    const message = createBaseBaseMatchCondition();
    message.readableDataKey = object.readableDataKey ?? '';
    message.action = object.action ?? 0;
    message.orGroupId = object.orGroupId ?? '';
    message.expression = object.expression ?? '';
    return message;
  },
};

function createBaseParentOverrideMatchCondition(): ParentOverrideMatchCondition {
  return { base: undefined, parentReadableId: '' };
}

export const ParentOverrideMatchCondition: MessageFns<ParentOverrideMatchCondition> = {
  encode(
    message: ParentOverrideMatchCondition,
    writer: BinaryWriter = new BinaryWriter()
  ): BinaryWriter {
    if (message.base !== undefined) {
      BaseMatchCondition.encode(message.base, writer.uint32(10).fork()).join();
    }
    if (message.parentReadableId !== '') {
      writer.uint32(18).string(message.parentReadableId);
    }
    return writer;
  },

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

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

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

  fromJSON(object: any): ParentOverrideMatchCondition {
    return {
      base: isSet(object.base) ? BaseMatchCondition.fromJSON(object.base) : undefined,
      parentReadableId: isSet(object.parentReadableId)
        ? globalThis.String(object.parentReadableId)
        : isSet(object.parent_readable_id)
          ? globalThis.String(object.parent_readable_id)
          : '',
    };
  },

  toJSON(message: ParentOverrideMatchCondition): unknown {
    const obj: any = {};
    if (message.base !== undefined) {
      obj.base = BaseMatchCondition.toJSON(message.base);
    }
    if (message.parentReadableId !== '') {
      obj.parentReadableId = message.parentReadableId;
    }
    return obj;
  },

  create(base?: DeepPartial<ParentOverrideMatchCondition>): ParentOverrideMatchCondition {
    return ParentOverrideMatchCondition.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<ParentOverrideMatchCondition>): ParentOverrideMatchCondition {
    const message = createBaseParentOverrideMatchCondition();
    message.base =
      object.base !== undefined && object.base !== null
        ? BaseMatchCondition.fromPartial(object.base)
        : undefined;
    message.parentReadableId = object.parentReadableId ?? '';
    return message;
  },
};

function createBaseSleepMatchCondition(): SleepMatchCondition {
  return { base: undefined, sleepFor: '' };
}

export const SleepMatchCondition: MessageFns<SleepMatchCondition> = {
  encode(message: SleepMatchCondition, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.base !== undefined) {
      BaseMatchCondition.encode(message.base, writer.uint32(10).fork()).join();
    }
    if (message.sleepFor !== '') {
      writer.uint32(18).string(message.sleepFor);
    }
    return writer;
  },

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

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

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

  fromJSON(object: any): SleepMatchCondition {
    return {
      base: isSet(object.base) ? BaseMatchCondition.fromJSON(object.base) : undefined,
      sleepFor: isSet(object.sleepFor)
        ? globalThis.String(object.sleepFor)
        : isSet(object.sleep_for)
          ? globalThis.String(object.sleep_for)
          : '',
    };
  },

  toJSON(message: SleepMatchCondition): unknown {
    const obj: any = {};
    if (message.base !== undefined) {
      obj.base = BaseMatchCondition.toJSON(message.base);
    }
    if (message.sleepFor !== '') {
      obj.sleepFor = message.sleepFor;
    }
    return obj;
  },

  create(base?: DeepPartial<SleepMatchCondition>): SleepMatchCondition {
    return SleepMatchCondition.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<SleepMatchCondition>): SleepMatchCondition {
    const message = createBaseSleepMatchCondition();
    message.base =
      object.base !== undefined && object.base !== null
        ? BaseMatchCondition.fromPartial(object.base)
        : undefined;
    message.sleepFor = object.sleepFor ?? '';
    return message;
  },
};

function createBaseUserEventMatchCondition(): UserEventMatchCondition {
  return {
    base: undefined,
    userEventKey: '',
    eventScope: undefined,
    considerEventsSince: undefined,
  };
}

export const UserEventMatchCondition: MessageFns<UserEventMatchCondition> = {
  encode(
    message: UserEventMatchCondition,
    writer: BinaryWriter = new BinaryWriter()
  ): BinaryWriter {
    if (message.base !== undefined) {
      BaseMatchCondition.encode(message.base, writer.uint32(10).fork()).join();
    }
    if (message.userEventKey !== '') {
      writer.uint32(18).string(message.userEventKey);
    }
    if (message.eventScope !== undefined) {
      writer.uint32(26).string(message.eventScope);
    }
    if (message.considerEventsSince !== undefined) {
      Timestamp.encode(toTimestamp(message.considerEventsSince), writer.uint32(34).fork()).join();
    }
    return writer;
  },

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

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

          message.userEventKey = reader.string();
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          message.eventScope = reader.string();
          continue;
        }
        case 4: {
          if (tag !== 34) {
            break;
          }

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

  fromJSON(object: any): UserEventMatchCondition {
    return {
      base: isSet(object.base) ? BaseMatchCondition.fromJSON(object.base) : undefined,
      userEventKey: isSet(object.userEventKey)
        ? globalThis.String(object.userEventKey)
        : isSet(object.user_event_key)
          ? globalThis.String(object.user_event_key)
          : '',
      eventScope: isSet(object.eventScope)
        ? globalThis.String(object.eventScope)
        : isSet(object.event_scope)
          ? globalThis.String(object.event_scope)
          : undefined,
      considerEventsSince: isSet(object.considerEventsSince)
        ? fromJsonTimestamp(object.considerEventsSince)
        : isSet(object.consider_events_since)
          ? fromJsonTimestamp(object.consider_events_since)
          : undefined,
    };
  },

  toJSON(message: UserEventMatchCondition): unknown {
    const obj: any = {};
    if (message.base !== undefined) {
      obj.base = BaseMatchCondition.toJSON(message.base);
    }
    if (message.userEventKey !== '') {
      obj.userEventKey = message.userEventKey;
    }
    if (message.eventScope !== undefined) {
      obj.eventScope = message.eventScope;
    }
    if (message.considerEventsSince !== undefined) {
      obj.considerEventsSince = message.considerEventsSince.toISOString();
    }
    return obj;
  },

  create(base?: DeepPartial<UserEventMatchCondition>): UserEventMatchCondition {
    return UserEventMatchCondition.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<UserEventMatchCondition>): UserEventMatchCondition {
    const message = createBaseUserEventMatchCondition();
    message.base =
      object.base !== undefined && object.base !== null
        ? BaseMatchCondition.fromPartial(object.base)
        : undefined;
    message.userEventKey = object.userEventKey ?? '';
    message.eventScope = object.eventScope ?? undefined;
    message.considerEventsSince = object.considerEventsSince ?? undefined;
    return message;
  },
};

function createBaseTaskConditions(): TaskConditions {
  return { parentOverrideConditions: [], sleepConditions: [], userEventConditions: [] };
}

export const TaskConditions: MessageFns<TaskConditions> = {
  encode(message: TaskConditions, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.parentOverrideConditions) {
      ParentOverrideMatchCondition.encode(v!, writer.uint32(10).fork()).join();
    }
    for (const v of message.sleepConditions) {
      SleepMatchCondition.encode(v!, writer.uint32(18).fork()).join();
    }
    for (const v of message.userEventConditions) {
      UserEventMatchCondition.encode(v!, writer.uint32(26).fork()).join();
    }
    return writer;
  },

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

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

          message.sleepConditions.push(SleepMatchCondition.decode(reader, reader.uint32()));
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

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

  fromJSON(object: any): TaskConditions {
    return {
      parentOverrideConditions: globalThis.Array.isArray(object?.parentOverrideConditions)
        ? object.parentOverrideConditions.map((e: any) => ParentOverrideMatchCondition.fromJSON(e))
        : globalThis.Array.isArray(object?.parent_override_conditions)
          ? object.parent_override_conditions.map((e: any) =>
              ParentOverrideMatchCondition.fromJSON(e)
            )
          : [],
      sleepConditions: globalThis.Array.isArray(object?.sleepConditions)
        ? object.sleepConditions.map((e: any) => SleepMatchCondition.fromJSON(e))
        : globalThis.Array.isArray(object?.sleep_conditions)
          ? object.sleep_conditions.map((e: any) => SleepMatchCondition.fromJSON(e))
          : [],
      userEventConditions: globalThis.Array.isArray(object?.userEventConditions)
        ? object.userEventConditions.map((e: any) => UserEventMatchCondition.fromJSON(e))
        : globalThis.Array.isArray(object?.user_event_conditions)
          ? object.user_event_conditions.map((e: any) => UserEventMatchCondition.fromJSON(e))
          : [],
    };
  },

  toJSON(message: TaskConditions): unknown {
    const obj: any = {};
    if (message.parentOverrideConditions?.length) {
      obj.parentOverrideConditions = message.parentOverrideConditions.map((e) =>
        ParentOverrideMatchCondition.toJSON(e)
      );
    }
    if (message.sleepConditions?.length) {
      obj.sleepConditions = message.sleepConditions.map((e) => SleepMatchCondition.toJSON(e));
    }
    if (message.userEventConditions?.length) {
      obj.userEventConditions = message.userEventConditions.map((e) =>
        UserEventMatchCondition.toJSON(e)
      );
    }
    return obj;
  },

  create(base?: DeepPartial<TaskConditions>): TaskConditions {
    return TaskConditions.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<TaskConditions>): TaskConditions {
    const message = createBaseTaskConditions();
    message.parentOverrideConditions =
      object.parentOverrideConditions?.map((e) => ParentOverrideMatchCondition.fromPartial(e)) ||
      [];
    message.sleepConditions =
      object.sleepConditions?.map((e) => SleepMatchCondition.fromPartial(e)) || [];
    message.userEventConditions =
      object.userEventConditions?.map((e) => UserEventMatchCondition.fromPartial(e)) || [];
    return message;
  },
};

function createBaseDurableEventListenerConditions(): DurableEventListenerConditions {
  return { sleepConditions: [], userEventConditions: [] };
}

export const DurableEventListenerConditions: MessageFns<DurableEventListenerConditions> = {
  encode(
    message: DurableEventListenerConditions,
    writer: BinaryWriter = new BinaryWriter()
  ): BinaryWriter {
    for (const v of message.sleepConditions) {
      SleepMatchCondition.encode(v!, writer.uint32(10).fork()).join();
    }
    for (const v of message.userEventConditions) {
      UserEventMatchCondition.encode(v!, writer.uint32(18).fork()).join();
    }
    return writer;
  },

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

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

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

  fromJSON(object: any): DurableEventListenerConditions {
    return {
      sleepConditions: globalThis.Array.isArray(object?.sleepConditions)
        ? object.sleepConditions.map((e: any) => SleepMatchCondition.fromJSON(e))
        : globalThis.Array.isArray(object?.sleep_conditions)
          ? object.sleep_conditions.map((e: any) => SleepMatchCondition.fromJSON(e))
          : [],
      userEventConditions: globalThis.Array.isArray(object?.userEventConditions)
        ? object.userEventConditions.map((e: any) => UserEventMatchCondition.fromJSON(e))
        : globalThis.Array.isArray(object?.user_event_conditions)
          ? object.user_event_conditions.map((e: any) => UserEventMatchCondition.fromJSON(e))
          : [],
    };
  },

  toJSON(message: DurableEventListenerConditions): unknown {
    const obj: any = {};
    if (message.sleepConditions?.length) {
      obj.sleepConditions = message.sleepConditions.map((e) => SleepMatchCondition.toJSON(e));
    }
    if (message.userEventConditions?.length) {
      obj.userEventConditions = message.userEventConditions.map((e) =>
        UserEventMatchCondition.toJSON(e)
      );
    }
    return obj;
  },

  create(base?: DeepPartial<DurableEventListenerConditions>): DurableEventListenerConditions {
    return DurableEventListenerConditions.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<DurableEventListenerConditions>): DurableEventListenerConditions {
    const message = createBaseDurableEventListenerConditions();
    message.sleepConditions =
      object.sleepConditions?.map((e) => SleepMatchCondition.fromPartial(e)) || [];
    message.userEventConditions =
      object.userEventConditions?.map((e) => UserEventMatchCondition.fromPartial(e)) || [];
    return message;
  },
};

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 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;
}
