import { HttpRequest } from '@angular/common/http';
import UrlPattern from 'url-pattern';

type IReq = 'get' | 'post' | 'put' | 'head' | 'delete' | 'patch' | 'options';

export type IMockCollectValue = {
  [key in IReq]?: IMockResponse | IMockData;
};

// 存储路由规则的数据格式
export interface IMockCollect {
  method: string;
  url: string;
  params?: ISafeAny;
  pattern: UrlPattern;
  query?: ISafeAny;
  value: ISafeAny;
}

// url 解析后返回到 拦截器中的数据类型
export interface IMockInfo extends IMockCollect {
  [key: string]: ISafeAny;
  method: string;
  url: string;
  params?: ISafeAny;
}

// mock 处理函数参数携带内容
export interface IMockRequest {
  params?: ISafeAny;
  query?: ISafeAny;
  _httpReq: HttpRequest<ISafeAny>;
}

// 全局配置 mock 属性
export interface IMockConfig {
  data: ISafeAny;
  log?: boolean;
  delay?: number;
  ignoreOtherInterceptors?: boolean;
  interceptors?: {
    response: (res: ISafeAny) => ISafeAny;
  };
}

// 具体某一个接口的配置数据
export interface IMockData {
  delay?: 300;
  disable?: boolean;
  value: IMockResponse;
}

export type IMockResponse = (res: IMockRequest) => ISafeAny;

// tslint:disable-next-line: no-any
export type ISafeAny = any;

export const REQUEST_METHOD = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'PATCH', 'OPTIONS'];

export interface IMock {
  [key: string]: IMockResponse | IMockCollectValue;
}
