// 将原始index.js文件中的元素类移动到src/elements目录下
// BaseElement.js
class BaseElement {
  constructor(elementType = 'div', engineId) {
    this.element = document.createElement(elementType);
    this.element.style.position = 'absolute';
    this.engineId = engineId;
    this.x = 0;
    this.y = 0;
    this.width = 0;
    this.height = 0;
  }

  setPosition(x = 0, y = 0) {
    this.element.style.left = `${x}px`;
    this.element.style.top = `${y}px`;
    this.x = x;
    this.y = y;
  }

  setSize(width = 0, height = 0) {
    if (width) {
      this.element.style.width = typeof width === 'number' ? `${width}px` : width;
      this.width = width;
    }
    if (height) {
      this.element.style.height = typeof height === 'number' ? `${height}px` : height;
      this.height = height;
    }
  }

  setStyle(style) {
    Object.assign(this.element.style, style);
  }

  add(child) {
    child && this.element.appendChild(child.element || child);
  }

  remove(child) {
    child && this.element.removeChild(child.element || child);
  }

  on(event, callback) {
    this.element.addEventListener(event, callback);
  }

  off(event, callback) {
    this.element.removeEventListener(event, callback);
  }

  hide() {
    this.element.style.display = 'none';
  }

  show() {
    this.element.style.display = 'block';
  }
}

export default BaseElement;
export { BaseElement };