"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
/**
 * helper functions for navigating in the scene graph of IG nodes
 * @author NM, TT
 */
/** Delimiter dividing super root name from gfx name parts */
exports.SuperRootDelimiter = "__";
/** Postfix for the root objects. */
exports.RootPostfix = "__.";
/** Postfix for the ghost objects. */
exports.GhostPostfix = "__gh";
/** Postfix for the platform objects */
exports.PlatformPostfix = "__pf";
exports.regexSuperRoot = /^(t[1-9][0-9]*)/;
exports.regexRoot = /^(t[1-9][0-9]*__(\.))/;
exports.regexObject = /^(t[1-9][0-9]*__(o[1-9][0-9]*)(\.o[1-9][0-9]*)*)$/;
exports.regexPlatform = /^(t[1-9][0-9]*__(pf[1-9][0-9]*))$/;
exports.regexGhost = /^(t[1-9][0-9]*__(gh[1-9][0-9]*))$/;
// TODO 2018-08-23 complete regex
// const regex = /^(t[1-9][0-9]*(__(\.|(o[1-9][0-9]*)(\.o[1-9][0-9]*)*|(pf[1-9][0-9]*)))?){1}$/;
// https://regexr.com/
// t
// t0
// t1
// t13
// t13__.
// t13__pf1
// t13__o1
// t13__o1.o1
// t13__o1.o2
// t2
// t2__.
// t2__pf1
// t2__gh1
// t2__o1
// t2__o1.o1
// t2__o1.o2
// t1s3__.
// t13__pf1
// t13__gh1
// t13__o1
// t13__o1.o1
// t13__o1.o2
// isSuperRootGfxId("t13") -> true
// isSuperRootGfxId("t13__.") -> false
// isSuperRootGfxId("t13__pf1") -> false
// isSuperRootGfxId("t13__o1") -> false
// isSuperRootGfxId("t13__o1.o1") -> false
// isSuperRootGfxId("t13__o1.o2") -> false
/**
 * checks if componentId belongs to a super root component
 * @param componentId
 */
function isSuperRootGfxId(componentId) {
    if (!componentId) {
        return false;
    }
    var delimiterIdx = componentId.indexOf(exports.SuperRootDelimiter);
    if (delimiterIdx > -1) {
        return false;
    }
    return true;
}
exports.isSuperRootGfxId = isSuperRootGfxId;
// getSuperRootGfxId("t13") -> "t13"
// getSuperRootGfxId("t13__.") -> "t13"
// getSuperRootGfxId("t13__pf1") -> "t13"
// getSuperRootGfxId("t13__o1") -> "t13"
// getSuperRootGfxId("t13__o1.o1") -> "t13"
// getSuperRootGfxId("t13__o1.o2") -> "t13"
function getSuperRootGfxId(componentId) {
    if (ig_utilities_1.isNullOrUndefined(componentId)) {
        return null;
    }
    var delimiterIdx = componentId.indexOf(exports.SuperRootDelimiter);
    if (delimiterIdx > -1) {
        var superRootGfxId = componentId.substring(0, delimiterIdx);
        return superRootGfxId;
    }
    return componentId;
}
exports.getSuperRootGfxId = getSuperRootGfxId;
// isRootGfxId("t13") -> false
// isRootGfxId("t13__.") -> true
// isRootGfxId("t13__pf1") -> false
// isRootGfxId("t13__o1") -> false
// isRootGfxId("t13__o1.o1") -> false
// isRootGfxId("t13__o1.o2") -> false
/**
 * checks if componentId belongs to a root component
 * @param componentId
 */
function isRootGfxId(componentId) {
    if (!componentId) {
        return false;
    }
    var delimiterIdx = componentId.indexOf(exports.RootPostfix);
    if (delimiterIdx > -1) {
        return true;
    }
    return false;
}
exports.isRootGfxId = isRootGfxId;
// getRootGfxId("t13") -> "t13__."
// getRootGfxId("t13__.") -> "t13__."
// getRootGfxId("t13__pf1") -> "t13__."
// getRootGfxId("t13__o1") -> "t13__."
// getRootGfxId("t13__o1.o1") -> "t13__."
// getRootGfxId("t13__o1.o2") -> "t13__."
function getRootGfxId(componentId) {
    var superRootGfxId = getSuperRootGfxId(componentId);
    if (superRootGfxId) {
        return (superRootGfxId + exports.RootPostfix);
    }
    return null;
}
exports.getRootGfxId = getRootGfxId;
// getRelativeGfxId("t13") -> null
// getRelativeGfxId("t13__.") -> "."
// getRelativeGfxId("t13__pf1") -> "pf1"
// getRelativeGfxId("t13__o1") -> "o1"
// getRelativeGfxId("t13__o1.o1") -> "o1.o1"
// getRelativeGfxId("t13__o1.o2") -> "o1.o2"
function getRelativeGfxId(componentId) {
    if (isSuperRootGfxId(componentId)) {
        return null;
    }
    if (ig_utilities_1.isNullOrUndefined(componentId)) {
        return null;
    }
    var beginDelimiter = componentId.indexOf(exports.SuperRootDelimiter);
    var result = componentId.substr(beginDelimiter + exports.SuperRootDelimiter.length);
    return result;
}
exports.getRelativeGfxId = getRelativeGfxId;
exports.getAbsoluteGfxId = function (path, superRootId) {
    if (path.indexOf(exports.SuperRootDelimiter) > -1) {
        return path;
    }
    return superRootId + "__" + path;
};
// getParentGfxId("t13") -> "t13"
// getParentGfxId("t13__.") -> "t13"
// getParentGfxId("t13__pf1") -> "t13"
// getParentGfxId("t13__gh1") -> "t13"
// getParentGfxId("t13__o1") -> "t13__."
// getParentGfxId("t13__o1.o1") -> "t13__o1"
// getParentGfxId("t13__o1.o2") -> "t13__o1"
// TODO 2018-08-23 implement getParentGfxId
function getParentGfxId(componentId) {
    if (!componentId) {
        return null;
    }
    if (isSuperRootGfxId(componentId)) {
        return null;
    }
    if (isRootGfxId(componentId) ||
        isPlatformGfxId(componentId) ||
        isGhostGfxId(componentId)) {
        return getSuperRootGfxId(componentId);
    }
    // probably only child components from here on
    var superRootParts = componentId.split(exports.SuperRootDelimiter);
    var childrenParts = superRootParts[1].split(".");
    if (childrenParts.length === 1) {
        return getRootGfxId(componentId);
    }
    childrenParts.pop();
    var result = superRootParts[0] + exports.SuperRootDelimiter + childrenParts.join(".");
    return result;
}
exports.getParentGfxId = getParentGfxId;
// fixIfSuperroot("t13") -> "t13__."
// fixIfSuperroot("t13__.") -> "t13__."
// fixIfSuperroot("t13__o1.o2") -> "t13__o1.o2"
// fixIfSuperroot("t13__pf1") -> "t13__pf1"
/**
 * gets root id if componentId is a super root, else just returns componentId
 * @param componentId
 */
function fixIfSuperRoot(componentId) {
    if (ig_utilities_1.isNullOrUndefined(componentId)) {
        return null;
    }
    if (isSuperRootGfxId(componentId)) {
        return getRootGfxId(componentId);
    }
    return componentId;
}
exports.fixIfSuperRoot = fixIfSuperRoot;
// isPlatformGfxId("t13") -> false
// isPlatformGfxId("t13__.") -> false
// isPlatformGfxId("t13__pf1") -> true
// isPlatformGfxId("t13__o1") -> false
// isPlatformGfxId("t13__o1.o1") -> false
// isPlatformGfxId("t13__o1.o2") -> false
/**
 * checks if componentId belongs to a platform component
 * @param componentId
 */
function isPlatformGfxId(componentId) {
    if (!componentId) {
        return false;
    }
    var delimiterIdx = componentId.indexOf(exports.PlatformPostfix);
    if (delimiterIdx > -1) {
        return true;
    }
    return false;
}
exports.isPlatformGfxId = isPlatformGfxId;
// isGhostGfxId("t13") -> false
// isGhostGfxId("t13__.") -> false
// isGhostGfxId("t13__pf1") -> false
// isGhostGfxId("t13__gh1") -> true
// isGhostGfxId("t13__o1") -> false
// isGhostGfxId("t13__o1.o1") -> false
// isGhostGfxId("t13__o1.o2") -> false
/**
 * checks if componentId belongs to a platform component
 * @param componentId
 */
function isGhostGfxId(componentId) {
    if (!componentId) {
        return false;
    }
    var delimiterIdx = componentId.indexOf(exports.GhostPostfix);
    if (delimiterIdx > -1) {
        return true;
    }
    return false;
}
exports.isGhostGfxId = isGhostGfxId;
exports.isChildGfxId = function (componentId, maybeParentId) {
    if (maybeParentId === componentId) {
        return false;
    }
    if (isRootGfxId(maybeParentId)) {
        maybeParentId = maybeParentId.slice(0, -1);
    }
    return componentId.indexOf(maybeParentId) === 0;
};
exports.isSelfOrChildGfxId = function (componentId, maybeParentOrSelfId) {
    return maybeParentOrSelfId === componentId ||
        exports.isChildGfxId(componentId, maybeParentOrSelfId);
};
