import {CoreString} from "../../../core/String";
export const ROOT_NAME = "/";
export class ObjectsController {
  constructor(scene) {
    this.scene = scene;
  }
  findObjectByMask(mask) {
    return this.findObjectByMaskInObject(mask, this.scene.threejsScene());
  }
  findObjectByMaskInObject(mask, object, objectPath = "") {
    for (let child of object.children) {
      const childName = this._removeTrailingOrHeadingSlash(child.name);
      objectPath = this._removeTrailingOrHeadingSlash(objectPath);
      const path = `${objectPath}/${childName}`;
      if (CoreString.matchMask(path, mask)) {
        return child;
      }
      const grandChild = this.findObjectByMaskInObject(mask, child, path);
      if (grandChild) {
        return grandChild;
      }
    }
  }
  objectsByMask(mask) {
    return this.objectsByMaskInObject(mask, this.scene.threejsScene(), [], "");
  }
  objectsByMaskInObject(mask, object, list = [], objectPath = "") {
    for (let child of object.children) {
      const childName = this._removeTrailingOrHeadingSlash(child.name);
      objectPath = this._removeTrailingOrHeadingSlash(objectPath);
      const path = `${objectPath}/${childName}`;
      if (CoreString.matchMask(path, mask)) {
        list.push(child);
      }
      this.objectsByMaskInObject(mask, child, list, path);
    }
    return list;
  }
  _removeTrailingOrHeadingSlash(objectName) {
    if (objectName[0] == "/") {
      objectName = objectName.substr(1);
    }
    if (objectName[objectName.length - 1] == "/") {
      objectName = objectName.substr(0, objectName.length - 1);
    }
    return objectName;
  }
}
