export function createDomainError<
  TName extends string,
  TCode extends string,
  TArgs extends unknown[],
>(name: TName, code: TCode, messageFactory: (...args: TArgs) => string) {
  const ErrorClass = class extends Error {
    override name: TName = name;
    code: TCode = code;
    constructor(...args: TArgs) {
      super(messageFactory(...args));
    }
  };
  Object.defineProperty(ErrorClass, "name", { value: name });
  return ErrorClass;
}

export const InsufficientPermissionError = createDomainError(
  "InsufficientPermissionError",
  "INSUFFICIENT_PERMISSION",
  (actorId: string, requiredPermission: string) =>
    `Actor ${actorId} lacks required permission: ${requiredPermission}`,
);
