/** @format */
import * as React from 'react';
import OSS from 'ali-oss';
import monent from 'moment';
import {GET_AUTH_OSS} from '../const/path';
import Axios from '../utils/xhr';
class CustomOss {
    expireDate: number;
    instance: OSS | null;
    constructor() {
        this.instance = null;
        this.expireDate = 0;
    }
    init() {
        return new Promise((resolve, reject) => {
            if (this.instance && this.expireDate > monent().unix()) {
                resolve();
                return;
            }
            Axios({url: GET_AUTH_OSS})
                .then(({code, Data, msg}) => {
                    if (code === 0) {
                        this.expireDate = monent()
                            .add(14, 'm')
                            .unix();
                        this.instance = new OSS({
                            region: 'oss-cn-shanghai',
                            bucket: 'gd-image',
                            accessKeyId: Data.AccessKeyId,
                            accessKeySecret: Data.AccessKeySecret,
                            stsToken: Data.SecurityToken,
                        });
                        resolve();
                    } else {
                        this.expireDate = 0;
                        this.instance = null;
                        reject(new Error(msg));
                    }
                })
                .catch(error => {
                    reject(error);
                });
        });
    }
    put(filename: string, file: File) {
        return new Promise((resolve, reject) => {
            if (this.instance) {
                this.instance
                    .put(filename, new Blob([file], {type: file.type}))
                    .then(result => {
                        resolve(result);
                    })
                    .catch(error => {
                        reject(error);
                    });
            }
        });
    }
}
const ossClinet = new CustomOss();
/**
 * oss 文件上传
 */
export const useOss = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (filename: string, file: File) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return new Promise((resolve, reject) => {
            ossClinet
                .init()
                .then(() => {
                    ossClinet
                        .put(filename, file)
                        .then(res => {
                            setState(preState => ({...preState, loading: false, error: null}));
                            resolve(res);
                        })
                        .catch(error => {
                            setState(preState => ({...preState, loading: false, error: error.message}));
                            reject(error);
                        });
                })
                .catch(error => {
                    setState(preState => ({...preState, loading: false, error: error.message}));
                    reject(error);
                });
        });
    };
    return [state, requestData] as const;
};
