export default {
	downloadFile (data, opts, callback) {
		if (typeof data === 'string') {
			downloadLink(data)
		} else {
			let _Blob = new Blob([data], { type: opts.type }),
				BlobURL = window.URL.createObjectURL(_Blob)
			downloadLink(BlobURL)
			window.URL.revokeObjectURL(BlobURL)
		}
		callback && callback()
		function downloadLink (url) {
			const link = document.createElement('a')
			link.href = url
			link.download = opts.name || ''
			link.style.display = 'none'
			document.body.appendChild(link)
			link.click()
			document.body.removeChild(link)
		}
	},
	copy (data, deep = true) {
		return deep ? JSON.parse(JSON.stringify(data)) : Object.assign(data)
	},
	getDate (dateType, dateString, calculate) {
		let result = dateType
		let d = dateString ? new Date(dateString) : new Date()
		calculate && (d = setDate(d, calculate))
		const week = ['日', '一', '二', '三', '四', '五', '六']
		if (dateType.length == 0) {
			return d.getTime()
		}
		if (dateType.includes('Y')) {
			result = result.replace(/Y+/g, d.getFullYear().toString())
		}
		if (dateType.includes('y')) {
			result = result.replace(/y+/g, (d.getFullYear() + '').slice(2, 4))
		}
		if (dateType.includes('M')) {
			result = result.replace(/M+/g, (101 + d.getMonth() * 1 + '').slice(1, 3))
		}
		if (dateType.includes('m')) {
			result = result.replace(/m+/g, (d.getMonth() * 1 + 1).toString())
		}
		if (dateType.includes('D')) {
			result = result.replace(/D+/g, (100 + d.getDate() * 1 + '').slice(1, 3))
		}
		if (dateType.includes('d')) {
			result = result.replace(/d+/g, d.getDate().toString())
		}
		if (dateType.includes('H')) {
			result = result.replace(/H+/g, (100 + d.getHours() * 1 + '').slice(1, 3))
		}
		if (dateType.includes('h')) {
			result = result.replace(/h+/g, d.getHours().toString())
		}
		if (dateType.includes('I')) {
			result = result.replace(/I+/g, (100 + d.getMinutes() * 1 + '').slice(1, 3))
		}
		if (dateType.includes('i')) {
			result = result.replace(/i+/g, d.getMinutes().toString())
		}
		if (dateType.includes('S')) {
			result = result.replace(/S+/g, (100 + d.getSeconds() * 1 + '').slice(1, 3))
		}
		if (dateType.includes('s')) {
			result = result.replace(/s+/g, d.getSeconds().toString())
		}
		if (dateType.includes('w') || dateType.includes('W')) {
			result = result.replace(/[wW]+/g, week[d.getDay() * 1])
		}
		return result

		function setDate (d, calculate = {}) {
			Object.keys(calculate).forEach((v) => {
				if (!setFn[v]) {
					return
				}

				let val = Number(calculate[v]) || 0
				let getDateMethod = d[getFn[v]]()
				let setDateMethod = d[setFn[v]](Number(getDateMethod) + val)
				d = new Date(setDateMethod)
			})
			return d
		}
	}
}
