import request from "@/utils/request"
import qs from "qs"
import config from "@/commons"

const {api:{asset}} =config

declare global {
    interface Navigator {
        msSaveBlob?: (blob: any, defaultName?: string) => boolean
    }
}

class AssetsActionManageService{
    async getCompareInfo(payload:any,autotip=false){
        let result = await request({ url: `${asset}/dataAssetApi/compareBusinessColumnAndMetadataColumn`, method: "post", data:qs.stringify(payload) },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async exportAssetsColumns(payload:any,autotip=false){
        await request({ url: `${asset}/dataAssetColumnApi/exportBusinessColumnsByDataAssetId`, method: "postdownload", data:qs.stringify(payload),responseType:'blob' },autotip).then((data:any)=>{
            let types = data.data.type
            var blob = new Blob([data.data], {type: types});
            let fileName = ''
            let contentDisposition = data.headers['content-disposition'];
            if (contentDisposition) {
                fileName = window.decodeURI(data.headers['content-disposition'].split('=')[1]);
            }
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(blob, fileName);
            }
            else {
                var csvURL = window.URL.createObjectURL(blob);
                var tempLink = document.createElement('a');
                tempLink.href = csvURL;
                tempLink.setAttribute('download', fileName);
                tempLink.setAttribute('target', '_blank');
                document.body.appendChild(tempLink);
                tempLink.click();
                document.body.removeChild(tempLink);
            }
        }).catch(error=>{
            console.log(error)
        })
    }
    
    async exportBusinessColumnsByDataAssetId(payload:any,autotip=false){
        await request({ url: `${asset}/dataAssetColumnApi/exportBusinessColumnsByDataAssetId`, method: "postdownload", data:qs.stringify(payload),responseType:'blob' },autotip).then((data:any)=>{
            let types = data.data.type
            var blob = new Blob([data.data], {type: types});
            let fileName = ''
            let contentDisposition = data.headers['content-disposition'];
            if (contentDisposition) {
                fileName = window.decodeURI(data.headers['content-disposition'].split('=')[1]);
            }
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(blob, fileName);
            }
            else {
                var csvURL = window.URL.createObjectURL(blob);
                var tempLink = document.createElement('a');
                tempLink.href = csvURL;
                tempLink.setAttribute('download', fileName);
                tempLink.setAttribute('target', '_blank');
                document.body.appendChild(tempLink);
                tempLink.click();
                document.body.removeChild(tempLink);
            }
        }).catch(error=>{
            console.log(error)
        })
    }
    

    async getAssetColumnDetail(payload:any,autotip=false){
        let result = await request({ url: `${asset}/dataAssetColumnApi/getDataAssetColumnDetail`, method: "post", data:qs.stringify(payload) },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

}

export default new AssetsActionManageService()
