// Standard error codes for all cloud drivers
export const ErrCode = {
    COOKIE_EXPIRED:    'COOKIE_EXPIRED',
    COOKIE_INVALID:    'COOKIE_INVALID',
    TOKEN_EXPIRED:     'TOKEN_EXPIRED',
    SHARE_NOT_FOUND:   'SHARE_NOT_FOUND',
    SHARE_EXPIRED:     'SHARE_EXPIRED',
    PASSWORD_REQUIRED: 'PASSWORD_REQUIRED',
    PASSWORD_WRONG:    'PASSWORD_WRONG',
    CAPACITY_FULL:     'CAPACITY_FULL',
    FILE_EXISTS:       'FILE_EXISTS',
    RATE_LIMITED:      'RATE_LIMITED',
    TRANSFER_FAILED:   'TRANSFER_FAILED',
    NETWORK_ERROR:     'NETWORK_ERROR',
    UNSUPPORTED:       'UNSUPPORTED',
    UNKNOWN:           'UNKNOWN',
} as const;

export type ErrorCode = typeof ErrCode[keyof typeof ErrCode];

const messages: Record<string, string> = {
    [ErrCode.COOKIE_EXPIRED]:    'Cookie已过期，请重新登录',
    [ErrCode.COOKIE_INVALID]:    'Cookie无效，请检查配置',
    [ErrCode.TOKEN_EXPIRED]:     'Token已过期，请刷新',
    [ErrCode.SHARE_NOT_FOUND]:   '分享链接不存在或已被删除',
    [ErrCode.SHARE_EXPIRED]:     '分享链接已过期',
    [ErrCode.PASSWORD_REQUIRED]: '需要提取码',
    [ErrCode.PASSWORD_WRONG]:    '提取码错误',
    [ErrCode.CAPACITY_FULL]:     '网盘容量不足',
    [ErrCode.RATE_LIMITED]:      '请求过于频繁，请稍后重试',
    [ErrCode.TRANSFER_FAILED]:   '转存失败',
    [ErrCode.NETWORK_ERROR]:     '网络请求失败',
    [ErrCode.UNKNOWN]:           '未知错误',
};

export function errorResponse(code: ErrorCode, detail?: string) {
    return {
        success: false,
        code,
        message: messages[code] + (detail ? ': ' + detail : ''),
    };
}

export class TransferError extends Error {
    code: ErrorCode;
    detail?: string;
    cookieExpired: boolean;

    constructor(code: ErrorCode, detail?: string) {
        super(messages[code] + (detail ? ': ' + detail : ''));
        this.code = code;
        this.detail = detail;
        this.cookieExpired = (code === ErrCode.COOKIE_EXPIRED || code === ErrCode.COOKIE_INVALID);
    }
}

/** Detect error code from driver result message (for untagged drivers) */
export function detectErrorCode(result: { message?: string; cookieExpired?: boolean }): ErrorCode | null {
    if (!result || !result.message) return null;
    if (result.cookieExpired) return ErrCode.COOKIE_EXPIRED;
    const msg = result.message.toLowerCase();
    if (msg.includes('cookie') || msg.includes('登录') || msg.includes('bdstoken')) return ErrCode.COOKIE_EXPIRED;
    if (msg.includes('不存在') || msg.includes('not found') || msg.includes('已删除')) return ErrCode.SHARE_NOT_FOUND;
    if (msg.includes('过期') || msg.includes('expired')) return ErrCode.SHARE_EXPIRED;
    if (msg.includes('提取码') || msg.includes('密码') || msg.includes('password')) return ErrCode.PASSWORD_WRONG;
    if (msg.includes('容量') || msg.includes('空间') || msg.includes('capacity')) return ErrCode.CAPACITY_FULL;
    if (msg.includes('频繁') || msg.includes('稍后') || msg.includes('rate')) return ErrCode.RATE_LIMITED;
    if (msg.includes('网络') || msg.includes('fetch') || msg.includes('timeout')) return ErrCode.NETWORK_ERROR;
    return ErrCode.TRANSFER_FAILED;
}
