"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.numberOrDefault = exports.harmonicArray = exports.rangeArray = exports.getPinNodeId__ = exports.getPinNodeId_ = exports.getPinNodeId = 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.INTERNAL = exports.EXTERNAL = exports.TYPE = exports.DATA = exports.MERGE = exports.O = exports.I = exports.OUTPUT = exports.INPUT = exports.LINK_SEPARATOR = exports.SEPARATOR = void 0;
exports.getDatumNodeIdFromDatumLinkId = getDatumNodeIdFromDatumLinkId;
exports.getTypeFromLinkPinNodeId = getTypeFromLinkPinNodeId;
exports.getPinIdFromLinkPinNodeId = getPinIdFromLinkPinNodeId;
exports.getIdFromDatumNodeId = getIdFromDatumNodeId;
exports.getPinIdClass = getPinIdClass;
exports.getInputNodeId = getInputNodeId;
exports.getOutputNodeId = getOutputNodeId;
exports.getSelfPinNodeId = getSelfPinNodeId;
exports.getLinkId = getLinkId;
exports.getPinLinkId = getPinLinkId;
exports.getPinLinkIdFromPinNodeId = getPinLinkIdFromPinNodeId;
exports.isNodeId = isNodeId;
exports.isInternalNodeId = isInternalNodeId;
exports.isOutputPinId = isOutputPinId;
exports.isUnitNodeId = isUnitNodeId;
exports.isPinId = isPinId;
exports.getUnitIdFromNodeId = getUnitIdFromNodeId;
exports.isLinkId = isLinkId;
exports.isPinLinkId = isPinLinkId;
exports.isDatumLinkId = isDatumLinkId;
exports.isMetaLinkId = isMetaLinkId;
exports.isExternalLinkId = isExternalLinkId;
exports.getErrNodeId = getErrNodeId;
exports.segmentErrNodeId = segmentErrNodeId;
exports.segmentLinkPinNodeId = segmentLinkPinNodeId;
exports.segmentLinkId = segmentLinkId;
exports.segmentPinLinkId = segmentPinLinkId;
exports.segmentDatumLinkId = segmentDatumLinkId;
exports.segmentMetaLinkId = segmentMetaLinkId;
exports.segmentExternalLinkId = segmentExternalLinkId;
exports.getPinNodeIdFromPinLinkId = getPinNodeIdFromPinLinkId;
exports.getUnitIdFromPinLinkId = getUnitIdFromPinLinkId;
exports.isInputPinId = isInputPinId;
exports.isPinNodeId = isPinNodeId;
exports.isLinkPinNodeId = isLinkPinNodeId;
exports.isErrPinId = isErrPinId;
exports.segmentCount = segmentCount;
exports.getSelectionId = getSelectionId;
exports.sameUnit = sameUnit;
exports.getDatumNodeId = getDatumNodeId;
exports.isDatumNodeId = isDatumNodeId;
exports.segmentDatumNodeId = segmentDatumNodeId;
exports.getMetadataNodeId = getMetadataNodeId;
exports.getTypeNodeId = getTypeNodeId;
exports.isMetadataNodeId = isMetadataNodeId;
exports.segmentMetadataId = segmentMetadataId;
exports.getMergeNodeId = getMergeNodeId;
exports.isMergeNodeId = isMergeNodeId;
exports.segmentMergeNodeId = segmentMergeNodeId;
exports.getIdFromMergeNodeId = getIdFromMergeNodeId;
exports.getExtNodeId = getExtNodeId;
exports.getExtNodeIdFromIntNodeId = getExtNodeIdFromIntNodeId;
exports.getIntNodeIdFromExtNodeId = getIntNodeIdFromExtNodeId;
exports.getIntNodeId = getIntNodeId;
exports.isExternalNodeId = isExternalNodeId;
exports.segmentPlugNodeId = segmentPlugNodeId;
exports.segmentInternalNodeId = segmentInternalNodeId;
exports.isBaseSpecId = isBaseSpecId;
exports.isBaseSpec = isBaseSpec;
exports.kebabToCamel = kebabToCamel;
exports.camelToKebab = camelToKebab;
exports.camelToSnake = camelToSnake;
exports.snakeToCamel = snakeToCamel;
exports.camelToDashed = camelToDashed;
exports.kebabCase = kebabCase;
const SELF_1 = require("../../constant/SELF");
const util_1 = require("../../spec/util");
const string_1 = require("../../util/string");
exports.SEPARATOR = '/';
exports.LINK_SEPARATOR = '_';
exports.INPUT = 'input';
exports.OUTPUT = 'output';
exports.I = 'i';
exports.O = 'o';
exports.MERGE = '@';
exports.DATA = '#';
exports.TYPE = '$';
exports.EXTERNAL = '%';
exports.INTERNAL = '^';
exports.ERR = '!';
const UNIT_ID_REGEX = `[^${exports.SEPARATOR}${exports.MERGE}${exports.DATA}${exports.TYPE}${exports.EXTERNAL}${exports.INTERNAL}]+`;
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)/${PIN_ID_REGEX}/[^${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 getDatumNodeIdFromDatumLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    return isDatumNodeId(source) ? source : target;
}
function getTypeFromLinkPinNodeId(linkPinNodeId) {
    return linkPinNodeId.split('/')[1];
}
function getPinIdFromLinkPinNodeId(nodeId) {
    return nodeId.split('/')[2];
}
function getIdFromDatumNodeId(nodeId) {
    const { datumId: id } = segmentDatumNodeId(nodeId);
    return id;
}
function getPinIdClass(pinNodeId) {
    if (isOutputPinId(pinNodeId)) {
        return 'output';
    }
    if (isInputPinId(pinNodeId)) {
        return 'input';
    }
    throw `Input with id "${pinNodeId}" was not found`;
}
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}`;
}
function getOutputNodeId(unitId, pinId) {
    return `${unitId}/output/${pinId}`;
}
function getSelfPinNodeId(unitId) {
    return getOutputNodeId(unitId, SELF_1.SELF);
}
function getLinkId(source, targetId) {
    return `${source}${exports.LINK_SEPARATOR}${targetId}`;
}
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);
    }
}
function getPinLinkIdFromPinNodeId(pinNodeId) {
    const { unitId, type, pinId } = segmentLinkPinNodeId(pinNodeId);
    return getPinLinkId(unitId, type, pinId);
}
function isNodeId(text) {
    return isUnitNodeId(text) || isPinNodeId(text);
}
function isInternalNodeId(nodeId) {
    return exports.internalRegex.test(nodeId);
}
function isOutputPinId(nodeId) {
    return exports.outputIdRegex.test(nodeId);
}
function isUnitNodeId(nodeId) {
    return !!exports.unitIdRegex.exec(nodeId);
}
function isPinId(nodeId) {
    if (nodeId) {
        return isInputPinId(nodeId) || isOutputPinId(nodeId);
    }
    else {
        return false;
    }
}
function getUnitIdFromNodeId(nodeId) {
    if (isUnitNodeId(nodeId)) {
        return nodeId;
    }
    else {
        const [unitId] = nodeId.split('/');
        return unitId;
    }
}
function isLinkId(linkId) {
    return !!exports.linkRegex.exec(linkId);
}
function isPinLinkId(linkId) {
    const [source, target] = linkId.split(exports.LINK_SEPARATOR);
    return ((isUnitNodeId(source) && isPinId(target)) ||
        (isPinId(source) && isUnitNodeId(target)));
}
function isDatumLinkId(linkId) {
    const [source, target] = linkId.split(exports.LINK_SEPARATOR);
    return ((isPinNodeId(source) && isDatumNodeId(target)) ||
        (isDatumNodeId(source) && isPinNodeId(target)));
}
function isMetaLinkId(linkId) {
    const [source, target] = linkId.split(exports.LINK_SEPARATOR);
    return isMetadataNodeId(source);
}
function isExternalLinkId(linkId) {
    const [source, target] = linkId.split(exports.LINK_SEPARATOR);
    return (isExternalNodeId(source) ||
        isExternalNodeId(target) ||
        isInternalNodeId(source) ||
        isInternalNodeId(target));
}
function getErrNodeId(unit_id) {
    return `${exports.ERR}/${unit_id}`;
}
function segmentErrNodeId(errNodeId) {
    const unitId = errNodeId.substr(2, errNodeId.length);
    return {
        unitId,
    };
}
function segmentLinkPinNodeId(pinNodeId) {
    const [unitId, type, pinId] = pinNodeId.split('/');
    return {
        unitId,
        type,
        pinId,
    };
}
function segmentLinkId(linkId) {
    const [source, target] = linkId.split(exports.LINK_SEPARATOR);
    return {
        source,
        target,
    };
}
function segmentPinLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    const unitId = isUnitNodeId(source) ? source : target;
    const pinNodeId = isUnitNodeId(source) ? target : source;
    return {
        unitId,
        pinNodeId,
    };
}
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,
    };
}
function segmentMetaLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    const nodeId = target;
    const metadataNodeId = source;
    return {
        nodeId,
        metadataNodeId,
    };
}
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,
    };
}
function getPinNodeIdFromPinLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    return isUnitNodeId(source) ? target : source;
}
function getUnitIdFromPinLinkId(linkId) {
    const { source, target } = segmentLinkId(linkId);
    return isUnitNodeId(source) ? source : target;
}
function isInputPinId(nodeId) {
    return exports.inputIdRegex.test(nodeId);
}
function isPinNodeId(nodeId) {
    // return isLinkPinNodeId(id) || isMergeNodeId(id) || isExternalNodeId(id)
    return isLinkPinNodeId(nodeId) || isMergeNodeId(nodeId);
}
function isLinkPinNodeId(nodeId) {
    return exports.pinIdRegex.test(nodeId);
}
function isErrPinId(pinNodeId) {
    return (isOutputPinId(pinNodeId) && segmentLinkPinNodeId(pinNodeId).pinId === 'err');
}
function segmentCount(path) {
    return path.split('/').length;
}
function getSelectionId(nodeId, type) {
    return `${nodeId}/selection/${type}`;
}
function sameUnit(pinId0, pinId1) {
    return getUnitIdFromNodeId(pinId0) === getUnitIdFromNodeId(pinId1);
}
function getDatumNodeId(nodeId) {
    return `${exports.DATA}/${nodeId}`;
}
function isDatumNodeId(nodeId) {
    return !!exports.dataRegex.exec(nodeId);
}
function segmentDatumNodeId(datumNodeId) {
    const id = datumNodeId.substr(2, datumNodeId.length);
    return {
        datumId: id,
    };
}
function getMetadataNodeId(nodeId, name) {
    return `${exports.TYPE}/${nodeId}/${name}`;
}
function getTypeNodeId(nodeId) {
    return `${exports.TYPE}/${nodeId}`;
}
function isMetadataNodeId(nodeId) {
    return !!exports.metadataRegex.exec(nodeId);
}
function segmentMetadataId(metadataNodeId) {
    const segments = tail(metadataNodeId.split('/'));
    const id = last(segments);
    const nodeId = dropLast(segments).join('/');
    return {
        nodeId,
        id,
    };
}
function getMergeNodeId(mergeId) {
    return `${exports.MERGE}/${mergeId}`;
}
function isMergeNodeId(nodeId) {
    return exports.mergeRegex.test(nodeId);
}
function segmentMergeNodeId(mergeNodeId) {
    // remove '@/'
    const mergeId = mergeNodeId.substring(2, mergeNodeId.length);
    return {
        mergeId,
    };
}
function getIdFromMergeNodeId(mergeNodeId) {
    const { mergeId } = segmentMergeNodeId(mergeNodeId);
    return mergeId;
}
function getExtNodeId(type, pinId, subPinId) {
    return `${exports.EXTERNAL}/${type}/${pinId}/${subPinId}`;
}
function getExtNodeIdFromIntNodeId(intNodeId) {
    const { type, pinId, subPinId } = segmentInternalNodeId(intNodeId);
    return getExtNodeId(type, pinId, subPinId);
}
function getIntNodeIdFromExtNodeId(ext_node_id) {
    const { type, pinId: pinId, subPinId } = segmentPlugNodeId(ext_node_id);
    return getIntNodeId(type, pinId, subPinId);
}
function getIntNodeId(type, pinId, subPinId) {
    return `${exports.INTERNAL}/${type}/${pinId}/${subPinId}`;
}
function isExternalNodeId(pinId) {
    return exports.externalRegex.test(pinId);
}
function segmentPlugNodeId(exposedNodeId) {
    const [_, type, pinId, subPinId] = exposedNodeId.split('/');
    return {
        type,
        pinId,
        subPinId,
    };
}
function segmentInternalNodeId(internalNodeId) {
    const [_, type, pinId, subPinId] = internalNodeId.split('/');
    return {
        type,
        pinId,
        subPinId,
    };
}
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 = (0, util_1.getSpec)(specs, id);
    return isBaseSpec(spec);
}
function isBaseSpec(spec) {
    const { base } = spec;
    return !!base;
}
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;
}
function camelToKebab(str) {
    const segments = str.split(/(?=[A-Z])/);
    const kebab = segments.map((_str) => _str.toLowerCase()).join('-');
    return kebab;
}
function camelToSnake(str) {
    const segments = str.split(/(?=[A-Z])/);
    const kebab = segments.map((_str) => _str.toLowerCase()).join('_');
    return kebab;
}
function snakeToCamel(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;
}
function camelToDashed(str) {
    const segments = str.split(/(?=[A-Z])/);
    const kebab = segments.map((_str) => _str.toLowerCase()).join('-');
    return kebab;
}
function kebabCase(str) {
    return str.toLowerCase().replace(' ', '-');
}
//# sourceMappingURL=index.js.map