"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLinkId = exports.getUnitIdFromNodeId = exports.isPinId = exports.isUnitNodeId = exports.isOutputPinId = exports.isInternalNodeId = exports.isNodeId = exports.getPinLinkIdFromPinNodeId = exports.getComponentLinkId = exports.getPinLinkId = exports.getLinkId = exports.getNodeLabelId = exports.getBoundaryNodeId = exports.getComponentNodeId = exports.getSelfPinNodeId = exports.getOutputNodeId = exports.getInputNodeId = exports.getPinNodeId__ = exports.getPinNodeId_ = exports.getPinNodeId = exports.getPinIdClass = exports.getIdFromDatumNodeId = exports.getPinIdFromLinkPinNodeId = exports.getTypeFromLinkPinNodeId = exports.getUnitIdFromPinId = exports.getDatumNodeIdFromDatumLinkId = exports.getUnitIdFromId = exports.linkRegex = exports.internalRegex = exports.externalRegex = exports.mergeRegex = exports.metadataRegex = exports.dataRegex = exports.outputIdRegex = exports.inputIdRegex = exports.pinIdRegex = exports.unitIdRegex = exports.nodeIdRegex = exports.idRegex = exports.ERR = exports.COMPONENT = exports.INTERNAL = exports.EXTERNAL = exports.TYPE = exports.DATA = exports.MERGE = exports.OUTPUT = exports.INPUT = exports.LINK_SEPARATOR = exports.SEPARATOR = void 0;
exports.kebabCase = exports.camelToSnake = exports.camelToKebab = exports.kebabToCamel = exports.numberOrDefault = exports.isBaseSpec = exports.isBaseSpecId = exports.harmonicArray = exports.rangeArray = exports.segmentInternalNodeId = exports.segmentPlugNodeId = exports.isExternalNodeId = exports.getIntNodeId = exports.getIntNodeIdFromExtNodeId = exports.getExtNodeIdFromIntNodeId = exports.getExtNodeId = exports.getIdFromMergeNodeId = exports.segmentMergeNodeId = exports.isMergeNodeId = exports.getMergeNodeId = exports.segmentMetadataId = exports.isMetadataNodeId = exports.getTypeNodeId = exports.getMetadataNodeId = exports.segmentDatumNodeId = exports.isDatumNodeId = exports.getDatumNodeId = exports.sameUnit = exports.getSelectionId = exports.segmentCount = exports.isErrPinId = exports.isLinkPinNodeId = exports.isPinNodeId = exports.isInputPinId = exports.getUnitIdFromPinLinkId = exports.getPinNodeIdFromPinLinkId = exports.segmentExternalLinkId = exports.segmentMetaLinkId = exports.segmentDatumLinkId = exports.segmentPinLinkId = exports.segmentLinkId = exports.segmentLinkPinNodeId = exports.segmentErrNodeId = exports.getErrNodeId = exports.isExternalLinkId = exports.isMetaLinkId = exports.isDatumLinkId = exports.isPinLinkId = void 0;
const SELF_1 = require("../../constant/SELF");
const string_1 = require("../../util/string");
exports.SEPARATOR = '/';
exports.LINK_SEPARATOR = '_';
exports.INPUT = 'input';
exports.OUTPUT = 'output';
exports.MERGE = '@';
exports.DATA = '#';
exports.TYPE = '$';
exports.EXTERNAL = '%';
exports.INTERNAL = '^';
exports.COMPONENT = '&';
exports.ERR = '!';
const UNIT_ID_REGEX = `[^${exports.SEPARATOR}${exports.MERGE}${exports.DATA}${exports.TYPE}${exports.EXTERNAL}]+`;
const PIN_ID_REGEX = `[^]+`;
const UNIT_NODE_ID_REGEX = `^(${UNIT_ID_REGEX})$`;
const PIN_NODE_ID_REGEX = `^(${UNIT_ID_REGEX})/(input|output)/(${PIN_ID_REGEX})$`;
const NODE_ID_REGEX = `((${UNIT_ID_REGEX})|(${UNIT_ID_REGEX})/(input|output)/(${PIN_ID_REGEX})|(${exports.MERGE}/${UNIT_ID_REGEX}))`;
const INPUT_NODE_ID_REGEX = `^(${UNIT_ID_REGEX})/input/(${PIN_ID_REGEX})$`;
const OUTPUT_NODE_ID_REGEX = `^(${UNIT_ID_REGEX})/output/(${PIN_ID_REGEX})$`;
exports.idRegex = new RegExp(`^${UNIT_ID_REGEX}$`);
exports.nodeIdRegex = new RegExp(`^${NODE_ID_REGEX}$`);
exports.unitIdRegex = new RegExp(UNIT_NODE_ID_REGEX);
exports.pinIdRegex = new RegExp(PIN_NODE_ID_REGEX);
exports.inputIdRegex = new RegExp(INPUT_NODE_ID_REGEX);
exports.outputIdRegex = new RegExp(OUTPUT_NODE_ID_REGEX);
exports.dataRegex = new RegExp(`^${exports.DATA}/[^#_]+$`);
exports.metadataRegex = new RegExp(`^\\$/[^]+/${UNIT_ID_REGEX}$`);
exports.mergeRegex = new RegExp(`^${exports.MERGE}/[^/#$]+$`);
exports.externalRegex = new RegExp(`^${exports.EXTERNAL}/(input|output)/[^${exports.SEPARATOR}${exports.DATA}${exports.MERGE}]+$`);
exports.internalRegex = new RegExp(`^\\${exports.INTERNAL}/(input|output)/${PIN_ID_REGEX}/[^${exports.SEPARATOR}${exports.DATA}${exports.MERGE}]+$`);
exports.linkRegex = new RegExp(`^(.+)${exports.LINK_SEPARATOR}(.+)$`);
function tail(array) {
    return array.slice(1, array.length);
}
function dropLast(array) {
    return array.slice(0, array.length - 1);
}
function last(array) {
    return array[array.length - 1];
}
function getUnitIdFromId(id) {
    return id.split('/')[0];
}
exports.getUnitIdFromId = getUnitIdFromId;
function getDatumNodeIdFromDatumLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    return isDatumNodeId(source) ? source : target;
}
exports.getDatumNodeIdFromDatumLinkId = getDatumNodeIdFromDatumLinkId;
function getUnitIdFromPinId(id) {
    return getUnitIdFromId(id);
}
exports.getUnitIdFromPinId = getUnitIdFromPinId;
function getTypeFromLinkPinNodeId(id) {
    return id.split('/')[1];
}
exports.getTypeFromLinkPinNodeId = getTypeFromLinkPinNodeId;
function getPinIdFromLinkPinNodeId(nodeId) {
    return nodeId.split('/')[2];
}
exports.getPinIdFromLinkPinNodeId = getPinIdFromLinkPinNodeId;
function getIdFromDatumNodeId(nodeId) {
    const { id } = segmentDatumNodeId(nodeId);
    return id;
}
exports.getIdFromDatumNodeId = getIdFromDatumNodeId;
function getPinIdClass(id) {
    if (isOutputPinId(id)) {
        return 'output';
    }
    if (isInputPinId(id)) {
        return 'input';
    }
    throw `Input with id "${id}" was not found`;
}
exports.getPinIdClass = getPinIdClass;
const getPinNodeId = (unitId, type, pinId) => {
    return `${unitId}/${type}/${pinId}`;
};
exports.getPinNodeId = getPinNodeId;
const getPinNodeId_ = (unitId) => {
    return (type, pinId) => `${unitId}/${type}/${pinId}`;
};
exports.getPinNodeId_ = getPinNodeId_;
const getPinNodeId__ = (unitId, type) => {
    return (pinId) => `${unitId}/${type}/${pinId}`;
};
exports.getPinNodeId__ = getPinNodeId__;
function getInputNodeId(unitId, pinId) {
    return `${unitId}/input/${pinId}`;
}
exports.getInputNodeId = getInputNodeId;
function getOutputNodeId(unitId, pinId) {
    return `${unitId}/output/${pinId}`;
}
exports.getOutputNodeId = getOutputNodeId;
function getSelfPinNodeId(unitId) {
    return getOutputNodeId(unitId, SELF_1.SELF);
}
exports.getSelfPinNodeId = getSelfPinNodeId;
const getComponentNodeId = (unitId) => {
    return `${exports.COMPONENT}/${unitId}`;
};
exports.getComponentNodeId = getComponentNodeId;
function getBoundaryNodeId(unitId, pinId) {
    return `${unitId}/boundary/${pinId}`;
}
exports.getBoundaryNodeId = getBoundaryNodeId;
function getNodeLabelId(id) {
    return `${id}/label`;
}
exports.getNodeLabelId = getNodeLabelId;
function getLinkId(source, targetId) {
    return `${source}${exports.LINK_SEPARATOR}${targetId}`;
}
exports.getLinkId = getLinkId;
function getPinLinkId(unitId, type, pinId) {
    const pinNodeId = (0, exports.getPinNodeId)(unitId, type, pinId);
    if (type === 'input') {
        return getLinkId(pinNodeId, unitId);
    }
    else {
        return getLinkId(unitId, pinNodeId);
    }
}
exports.getPinLinkId = getPinLinkId;
function getComponentLinkId(unitId) {
    const componentNodeId = (0, exports.getComponentNodeId)(unitId);
    return getLinkId(unitId, componentNodeId);
}
exports.getComponentLinkId = getComponentLinkId;
function getPinLinkIdFromPinNodeId(pinNodeId) {
    const { unitId, type, pinId } = segmentLinkPinNodeId(pinNodeId);
    return getPinLinkId(unitId, type, pinId);
}
exports.getPinLinkIdFromPinNodeId = getPinLinkIdFromPinNodeId;
function isNodeId(id) {
    return isUnitNodeId(id) || isPinNodeId(id);
}
exports.isNodeId = isNodeId;
function isInternalNodeId(id) {
    return exports.internalRegex.test(id);
}
exports.isInternalNodeId = isInternalNodeId;
function isOutputPinId(id) {
    return exports.outputIdRegex.test(id);
}
exports.isOutputPinId = isOutputPinId;
function isUnitNodeId(nodeId) {
    return !!exports.unitIdRegex.exec(nodeId);
}
exports.isUnitNodeId = isUnitNodeId;
function isPinId(id) {
    if (id) {
        return isInputPinId(id) || isOutputPinId(id);
    }
    else {
        return false;
    }
}
exports.isPinId = isPinId;
function getUnitIdFromNodeId(nodeId) {
    if (isUnitNodeId(nodeId)) {
        return nodeId;
    }
    else {
        const [unitId] = nodeId.split('/');
        return unitId;
    }
}
exports.getUnitIdFromNodeId = getUnitIdFromNodeId;
function isLinkId(id) {
    return !!exports.linkRegex.exec(id);
}
exports.isLinkId = isLinkId;
function isPinLinkId(id) {
    const [source, target] = id.split(exports.LINK_SEPARATOR);
    return ((isUnitNodeId(source) && isPinId(target)) ||
        (isPinId(source) && isUnitNodeId(target)));
}
exports.isPinLinkId = isPinLinkId;
function isDatumLinkId(id) {
    const [source, target] = id.split(exports.LINK_SEPARATOR);
    return ((isPinNodeId(source) && isDatumNodeId(target)) ||
        (isDatumNodeId(source) && isPinNodeId(target)));
}
exports.isDatumLinkId = isDatumLinkId;
function isMetaLinkId(id) {
    const [source, target] = id.split(exports.LINK_SEPARATOR);
    return isMetadataNodeId(source);
}
exports.isMetaLinkId = isMetaLinkId;
function isExternalLinkId(id) {
    const [source, target] = id.split(exports.LINK_SEPARATOR);
    return (isExternalNodeId(source) ||
        isExternalNodeId(target) ||
        isInternalNodeId(source) ||
        isInternalNodeId(target));
}
exports.isExternalLinkId = isExternalLinkId;
function getErrNodeId(unit_id) {
    return `${exports.ERR}/${unit_id}`;
}
exports.getErrNodeId = getErrNodeId;
function segmentErrNodeId(errNodeId) {
    const unitId = errNodeId.substr(2, errNodeId.length);
    return {
        unitId,
    };
}
exports.segmentErrNodeId = segmentErrNodeId;
function segmentLinkPinNodeId(pinNodeId) {
    const [unitId, type, pinId] = pinNodeId.split('/');
    return {
        unitId,
        type,
        pinId,
    };
}
exports.segmentLinkPinNodeId = segmentLinkPinNodeId;
function segmentLinkId(linkId) {
    const [source, target] = linkId.split(exports.LINK_SEPARATOR);
    return {
        source,
        target,
    };
}
exports.segmentLinkId = segmentLinkId;
function segmentPinLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    const unitId = isUnitNodeId(source) ? source : target;
    const pinNodeId = isUnitNodeId(source) ? target : source;
    return {
        unitId,
        pinNodeId,
    };
}
exports.segmentPinLinkId = segmentPinLinkId;
function segmentDatumLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    const sourceIsDatum = isDatumNodeId(source);
    const datumNodeId = sourceIsDatum ? source : target;
    const pinNodeId = sourceIsDatum ? target : source;
    return {
        datumNodeId,
        pinNodeId,
    };
}
exports.segmentDatumLinkId = segmentDatumLinkId;
function segmentMetaLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    const nodeId = target;
    const metadataNodeId = source;
    return {
        nodeId,
        metadataNodeId,
    };
}
exports.segmentMetaLinkId = segmentMetaLinkId;
function segmentExternalLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    const sourceExternal = isExternalNodeId(source);
    const externalNodeId = sourceExternal ? target : source;
    const nodeId = sourceExternal ? source : target;
    return {
        nodeId,
        externalNodeId,
    };
}
exports.segmentExternalLinkId = segmentExternalLinkId;
function getPinNodeIdFromPinLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    return isUnitNodeId(source) ? target : source;
}
exports.getPinNodeIdFromPinLinkId = getPinNodeIdFromPinLinkId;
function getUnitIdFromPinLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    return isUnitNodeId(source) ? source : target;
}
exports.getUnitIdFromPinLinkId = getUnitIdFromPinLinkId;
function isInputPinId(id) {
    return exports.inputIdRegex.test(id);
}
exports.isInputPinId = isInputPinId;
function isPinNodeId(id) {
    // return isLinkPinNodeId(id) || isMergeNodeId(id) || isExternalNodeId(id)
    return isLinkPinNodeId(id) || isMergeNodeId(id);
}
exports.isPinNodeId = isPinNodeId;
function isLinkPinNodeId(id) {
    return exports.pinIdRegex.test(id);
}
exports.isLinkPinNodeId = isLinkPinNodeId;
function isErrPinId(pinNodeId) {
    return (isOutputPinId(pinNodeId) && segmentLinkPinNodeId(pinNodeId).pinId === 'err');
}
exports.isErrPinId = isErrPinId;
function segmentCount(path) {
    return path.split('/').length;
}
exports.segmentCount = segmentCount;
function getSelectionId(nodeId, type) {
    return `${nodeId}/selection/${type}`;
}
exports.getSelectionId = getSelectionId;
function sameUnit(id0, id1) {
    return getUnitIdFromNodeId(id0) === getUnitIdFromNodeId(id1);
}
exports.sameUnit = sameUnit;
function getDatumNodeId(id) {
    return `${exports.DATA}/${id}`;
}
exports.getDatumNodeId = getDatumNodeId;
function isDatumNodeId(nodeId) {
    return !!exports.dataRegex.exec(nodeId);
}
exports.isDatumNodeId = isDatumNodeId;
function segmentDatumNodeId(datumNodeId) {
    // remove /#
    const id = datumNodeId.substr(2, datumNodeId.length);
    return {
        id,
    };
}
exports.segmentDatumNodeId = segmentDatumNodeId;
function getMetadataNodeId(nodeId, id) {
    return `${exports.TYPE}/${nodeId}/${id}`;
}
exports.getMetadataNodeId = getMetadataNodeId;
function getTypeNodeId(nodeId) {
    return `${exports.TYPE}/${nodeId}`;
}
exports.getTypeNodeId = getTypeNodeId;
function isMetadataNodeId(nodeId) {
    return !!exports.metadataRegex.exec(nodeId);
}
exports.isMetadataNodeId = isMetadataNodeId;
function segmentMetadataId(metadataNodeId) {
    const segments = tail(metadataNodeId.split('/'));
    const id = last(segments);
    const nodeId = dropLast(segments).join('/');
    return {
        nodeId,
        id,
    };
}
exports.segmentMetadataId = segmentMetadataId;
function getMergeNodeId(id) {
    return `${exports.MERGE}/${id}`;
}
exports.getMergeNodeId = getMergeNodeId;
function isMergeNodeId(id) {
    return exports.mergeRegex.test(id);
}
exports.isMergeNodeId = isMergeNodeId;
function segmentMergeNodeId(mergeNodeId) {
    // remove '@/'
    const mergeId = mergeNodeId.substring(2, mergeNodeId.length);
    return {
        mergeId,
    };
}
exports.segmentMergeNodeId = segmentMergeNodeId;
function getIdFromMergeNodeId(mergeNodeId) {
    const { mergeId: id } = segmentMergeNodeId(mergeNodeId);
    return id;
}
exports.getIdFromMergeNodeId = getIdFromMergeNodeId;
function getExtNodeId(type, id, subPinId) {
    return `${exports.EXTERNAL}/${type}/${id}/${subPinId}`;
}
exports.getExtNodeId = getExtNodeId;
function getExtNodeIdFromIntNodeId(intNodeId) {
    const { type, pinId, subPinId } = segmentInternalNodeId(intNodeId);
    return getExtNodeId(type, pinId, subPinId);
}
exports.getExtNodeIdFromIntNodeId = getExtNodeIdFromIntNodeId;
function getIntNodeIdFromExtNodeId(ext_node_id) {
    const { type, pinId: id, subPinId } = segmentPlugNodeId(ext_node_id);
    return getIntNodeId(type, id, subPinId);
}
exports.getIntNodeIdFromExtNodeId = getIntNodeIdFromExtNodeId;
function getIntNodeId(type, id, subPinId) {
    return `${exports.INTERNAL}/${type}/${id}/${subPinId}`;
}
exports.getIntNodeId = getIntNodeId;
function isExternalNodeId(id) {
    return exports.externalRegex.test(id);
}
exports.isExternalNodeId = isExternalNodeId;
function segmentPlugNodeId(exposedNodeId) {
    const [_, type, pinId, subPinId] = exposedNodeId.split('/');
    return {
        type,
        pinId,
        subPinId,
    };
}
exports.segmentPlugNodeId = segmentPlugNodeId;
function segmentInternalNodeId(internalNodeId) {
    const [_, type, pinId, subPinId] = internalNodeId.split('/');
    return {
        type,
        pinId,
        subPinId,
    };
}
exports.segmentInternalNodeId = segmentInternalNodeId;
const rangeArray = (n) => {
    const range = [];
    for (let i = 0; i < n; i++) {
        range.push(i);
    }
    return range;
};
exports.rangeArray = rangeArray;
const harmonicArray = (n) => {
    if (n === 0) {
        return [];
    }
    const array = [0];
    for (let i = 1; i < n; i++) {
        array.push(array[i - 1] + 1 / i);
    }
    return array;
};
exports.harmonicArray = harmonicArray;
function isBaseSpecId(specs, id) {
    const spec = specs[id];
    return isBaseSpec(spec);
}
exports.isBaseSpecId = isBaseSpecId;
function isBaseSpec(spec) {
    const { base } = spec;
    return !!base;
}
exports.isBaseSpec = isBaseSpec;
const numberOrDefault = (n, def) => {
    if (typeof n === 'number') {
        return n;
    }
    return def;
};
exports.numberOrDefault = numberOrDefault;
function kebabToCamel(str) {
    const segments = str.split(/\s*-\s*/);
    const first_segment = segments[0];
    const segments_tail = segments.slice(1);
    const camelled_segments_tail = segments_tail.map((_str) => (0, string_1.upperCaseFirstLetter)(_str));
    const camel = first_segment + camelled_segments_tail.join('');
    return camel;
}
exports.kebabToCamel = kebabToCamel;
function camelToKebab(str) {
    const segments = str.split(/(?=[A-Z])/);
    const kebab = segments.map((_str) => _str.toLowerCase()).join('-');
    return kebab;
}
exports.camelToKebab = camelToKebab;
function camelToSnake(str) {
    const segments = str.split(/(?=[A-Z])/);
    const kebab = segments.map((_str) => _str.toLowerCase()).join('_');
    return kebab;
}
exports.camelToSnake = camelToSnake;
function kebabCase(str) {
    return str.toLowerCase().replace(' ', '-');
}
exports.kebabCase = kebabCase;
//# sourceMappingURL=index.js.map