import isestr from './isestr.mjs'
import isu8arr from './isu8arr.mjs'
import u8arr2blob from './u8arr2blob.mjs'
import isWindow from './isWindow.mjs'
import downloadFileFromBlob from './downloadFileFromBlob.mjs'
/**
* 前端下載binary資料成為檔案
*
* Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/downloadFileFromU8Arr.test.mjs Github}
* @memberOf wsemi
* @param {String} fileName 輸入檔名字串
* @param {Uint8Array} u8a 輸入Uint8Array資料
* @example
* need test in browser
*
* //icon.png to u8a
* let u8a = new Uint8Array([137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,16,0,0,0,16,8,6,0,0,0,31,243,255,97,0,0,2,26,73,68,65,84,56,79,165,147,205,107,19,65,24,198,103,246,43,18,77,104,161,246,32,130,85,17,10,161,219,209,10,193,116,183,205,65,4,207,106,15,85,235,110,18,163,8,122,20,188,249,23,120,81,168,20,178,155,84,236,37,96,241,224,69,68,73,119,99,90,136,216,77,217,42,138,80,4,47,86,90,48,107,155,54,179,51,178,129,148,198,172,141,224,220,230,253,248,241,242,60,239,11,193,127,62,216,169,127,62,158,58,28,114,55,195,17,227,233,178,95,173,47,192,144,198,187,67,60,127,8,189,201,217,150,148,152,0,0,30,160,20,23,80,49,103,255,9,241,5,88,178,122,15,82,74,72,128,159,133,117,87,30,156,211,50,139,210,53,21,8,244,37,122,61,253,109,55,164,13,176,36,167,174,48,53,252,10,239,103,123,128,75,147,148,226,73,100,78,127,250,56,156,8,213,32,185,142,204,236,131,191,2,150,228,9,209,165,220,113,100,106,179,20,220,103,150,227,223,123,237,131,171,171,99,249,188,235,53,45,142,36,206,49,16,84,197,130,86,106,66,90,38,176,70,149,91,117,71,200,158,126,55,181,225,39,88,121,40,205,11,193,250,13,209,208,31,181,1,172,216,213,94,192,114,23,6,13,125,114,47,103,44,73,189,3,193,214,19,209,156,89,247,234,118,38,120,31,83,16,203,194,35,162,161,63,223,11,80,145,148,75,24,66,251,148,161,55,108,221,1,88,195,138,76,1,67,81,81,51,155,128,249,232,229,112,23,23,160,253,69,173,218,140,85,100,117,148,165,160,22,49,245,133,22,64,229,140,210,239,10,236,192,201,66,38,239,37,202,67,233,32,23,196,73,0,8,192,27,66,166,169,75,69,74,142,67,130,23,6,222,230,190,180,0,188,79,69,86,239,58,216,121,24,43,229,55,45,89,189,72,9,249,192,176,144,33,0,156,64,115,217,103,126,86,182,184,80,142,167,123,120,188,61,182,198,245,105,221,238,138,130,140,236,227,134,125,178,114,115,157,237,203,118,145,175,73,150,212,102,154,2,182,77,224,5,172,145,212,81,72,241,89,66,153,99,144,97,166,32,33,144,66,154,130,12,89,33,219,244,5,42,117,216,68,15,242,249,252,237,176,243,179,26,221,194,245,134,80,129,125,124,84,248,241,171,20,177,243,206,63,221,66,167,11,221,157,255,13,198,21,222,17,48,199,189,166,0,0,0,0,73,69,78,68,174,66,96,130])
* downloadFileFromU8Arr('icon.png',u8a)
*
*/
function downloadFileFromU8Arr(fileName, u8a) {
//check
if (!isWindow()) {
console.log('no window')
return
}
//check
if (!isestr(fileName)) {
console.log('no filename')
return
}
if (!isu8arr(u8a)) {
console.log('no data')
return
}
//轉blob
let blob = u8arr2blob(u8a)
//downloadFileFromBlob
downloadFileFromBlob(fileName, blob)
}
export default downloadFileFromU8Arr