import { ShadeError } from '@shade/core';

export class StreamError extends ShadeError {
  constructor(code: string, message: string) {
    super(code, message);
    this.name = 'StreamError';
  }
}

export class StreamProtocolError extends StreamError {
  constructor(message: string) {
    super('SHADE_STREAM_PROTOCOL', message);
    this.name = 'StreamProtocolError';
  }
}

export class StreamIntegrityError extends StreamError {
  constructor(message: string) {
    super('SHADE_STREAM_INTEGRITY', message);
    this.name = 'StreamIntegrityError';
  }
}

export class StreamReplayError extends StreamError {
  constructor(message = 'Stream chunk replay detected') {
    super('SHADE_STREAM_REPLAY', message);
    this.name = 'StreamReplayError';
  }
}

export class StreamOutOfOrderError extends StreamError {
  constructor(expected: number, received: number) {
    super(
      'SHADE_STREAM_OUT_OF_ORDER',
      `Out-of-order chunk: expected seq=${expected}, got ${received}`,
    );
    this.name = 'StreamOutOfOrderError';
  }
}

export class StreamDecryptionError extends StreamError {
  constructor(message = 'Stream chunk authenticated decryption failed') {
    super('SHADE_STREAM_DECRYPTION', message);
    this.name = 'StreamDecryptionError';
  }
}

export class StreamFinishedError extends StreamError {
  constructor(message = 'Stream is already finished or aborted') {
    super('SHADE_STREAM_FINISHED', message);
    this.name = 'StreamFinishedError';
  }
}
