import basic from './basic';
import listen from './listen';
import style from './style';
import errors from './error/main';

class edoElement {
	[Symbol.toStringTag]: string = 'edoElement';
	el: HTMLElement = document.createElement('div');
	constructor(val: string | HTMLElement) {
		if (typeof val === 'string') {
			try {
				if (document.querySelector(val as string) === null) {
					throw new errors.edoHTMLElementIsNotFoundError(`The element "${val}" is not found!!!`);
				} else {
					this.el = document.querySelector(val as string) as HTMLElement;
				}
			} catch (e: unknown) {
				if (!(e as Error).name.startsWith('edo')) {
					throw new errors.edoCssSelectorError(`This Css Selector "${val}" is error`);
				}
				else {
					throw e as Error;
				}
			}
		} else if (val instanceof HTMLElement) {
			this.el = val as HTMLElement;
		} else {
			throw new errors.edoTypeError(`This type should is HTMLElement or string, fault ${Object.prototype.toString.call(val).split(' ')[1].replace(']', '')}`);
		}
		this.el;
	}
}

const edo = function (val: string | HTMLElement): edoElement {
	return new edoElement(val);
};
edo.proto = function (name: string, value: any): boolean {
	try {
		edoElement.prototype[name] = value;
		return true;
	} catch {
		return false;
	}
};

basic(edo);
listen(edo);
style(edo);

export default edo;
