import edoTypeError from './error/edoTypeError';

export default function (edo: { proto: (name: string, value: any) => boolean; [key: string]: any }) {
	edo.css = function (options: object | string, a2: object | string | undefined): void {
		const head = document.querySelector('head');
		let str = '';
		if (options instanceof Object) {
			for (let s in options as object) {
				str += `${s}{`;
				let temp = options[s];
				for (let s2 in temp) {
					str += `${s2}:${temp[s2]};`;
				}
				str += '}';
			}
		} else if (typeof options === 'string') {
			str += `${options}{`;
			if (typeof a2 === 'string') {
				str += a2;
			} else if ((a2 as any) instanceof Object) {
				for (let s in a2 as object) {
					str += `${s}:${(a2 as object)[s]};`;
				}
			} else {
				throw new edoTypeError(`This type should is Object or string, fault ${Object.prototype.toString.call(a2).split(' ')[1].replace(']', '')}`);
			}
			str += `}`;
		} else {
			throw new edoTypeError(`This type should is Object or string, fault ${Object.prototype.toString.call(options).split(' ')[1].replace(']', '')}`);
		}
		(head as HTMLHeadElement).innerHTML += `<style>${str}</style>`;
	};
	edo.proto('css', function (a1: object | string, a2: string | undefined): boolean {
		if (a2 === undefined) {
			if (typeof a1 === 'string') {
				this.el.style = a1;
			} else {
				for (let s in a1) {
					this.el.style[s] = a1[s];
				}
			}
		} else {
			this.el.style[a1 as string] = a2;
		}
		return true;
	});
	edo.proto('getCss', function (name: string): string {
		return this.el.style[name];
	});
}
