import {NameController as NameController2} from "../NameController";
import {CoreWalker} from "../../../../core/Walker";
export class HierarchyParentController {
  constructor(node) {
    this.node = node;
    this._parent = null;
  }
  parent() {
    return this._parent;
  }
  setParent(parent) {
    if (parent != this.node.parentController.parent()) {
      this._parent = parent;
      if (this._parent) {
        this.node.nameController.request_name_to_parent(NameController2.base_name(this.node));
      }
    }
  }
  is_selected() {
    return this.parent()?.childrenController?.selection?.contains(this.node) || false;
  }
  fullPath(relative_to_parent) {
    const separator = CoreWalker.SEPARATOR;
    if (this._parent != null) {
      if (this._parent == relative_to_parent) {
        return this.node.name();
      } else {
        const parent_fullPath = this._parent.fullPath(relative_to_parent);
        if (parent_fullPath === separator) {
          return parent_fullPath + this.node.name();
        } else {
          return parent_fullPath + separator + this.node.name();
        }
      }
    } else {
      return separator;
    }
  }
  onSetParent() {
    if (this._on_set_parent_hooks) {
      for (let hook of this._on_set_parent_hooks) {
        hook();
      }
    }
  }
  find_node(path) {
    if (path == null) {
      return null;
    }
    if (path == CoreWalker.CURRENT || path == CoreWalker.CURRENT_WITH_SLASH) {
      return this.node;
    }
    if (path == CoreWalker.PARENT || path == CoreWalker.PARENT_WITH_SLASH) {
      return this.node.parent();
    }
    const separator = CoreWalker.SEPARATOR;
    if (path[0] === separator) {
      path = path.substring(1, path.length);
    }
    if (path.split) {
      const elements = path.split(separator);
      if (elements.length === 1) {
        const name = elements[0];
        if (this.node.childrenController) {
          return this.node.childrenController.child_by_name(name);
        } else {
          return null;
        }
      } else {
        return CoreWalker.find_node(this.node, path);
      }
    } else {
      console.error("unexpected path given:", path);
      return null;
    }
  }
}
