import { tools } from 'hq-tools';

export const dict = {
	valueFormatter: params => {
		let dict = params.colDef.cellRendererParams ? params.colDef.cellRendererParams.dict : null;
		if (dict) {
			if (dict instanceof Map) {
				return dict.get(params.value) ? dict.get(params.value) : params.value;
			} else if (dict instanceof Object) {
				return dict[params.value] ? dict[params.value] : params.value;
			} else if (typeof dict === 'string') {
				return !params.context.dicts[dict] ? params.value : params.context.dicts[dict][params.value] ? params.context.dicts[dict][params.value] : params.value;
			}
		} else {
			console.warn(`cannot find valid dict config at row ${params.node.childIndex}`);
			return params.value;
		}
	}
};

export const number = {
	valueFormatter: params => {
		let format = params.colDef.cellRendererParams ? params.colDef.cellRendererParams.format : null;
		return (format && params.value) ? tools.partition(params.value, format) : params.value;
	},
	filter: 'number'
};
export const string = {

};
export const image = {
	cellRendererFramework: 'ImageCell'
};
export const qrcode = {
	cellRendererFramework: 'QrcodeCell'
};
export const datetime = {
	valueFormatter: params => {
		return tools.dateFormat(params.value, 'yyyy-MM-dd HH:mm:ss');
	}
};
export const date = {
	valueFormatter: params => {
		return new Date(params.value) === 'Invalid Date'?params.value:tools.dateFormat(params.value, 'yyyy-MM-dd');
	},
	toolTip: value => {
		return tools.dateFormat(value, 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss');
	}
};
export const boolean = {
	valueFormatter: params => (!params.value ? '《无》' : ['1', true].includes(params.value) ? '是' : '否'),
	cellRendererFramework: 'BooleanCell'
};
export const button = {
	cellRendererFramework: 'ButtonCell',
	filter: false,
	print: false
};
export const asyncTrans = {
	valueFormatter: params => {
		let dict = params.context.gridDicts[params.colDef.colId];
		if (dict) {
			return dict[params.value] ? dict[params.value] : params.value;
		} else {
			return params.value;
		}
	}
};
export default {
	dict,
	number,
	string,
	image,
	qrcode,
	datetime,
	date,
	boolean,
	button,
	asyncTrans
};
