{
    "Function": "create",
    "File": "registries/contracts/UnitRegistry.sol",
    "Parent Contracts": [
        "registries/contracts/GenericRegistry.sol",
        "registries/lib/solmate/src/tokens/ERC721.sol",
        "registries/contracts/interfaces/IErrorsRegistries.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [
        "_checkDependencies",
        "revert ZeroValue()",
        "revert ZeroAddress()",
        "_safeMint",
        "revert ManagerOnly(address,address)",
        "_calculateSubComponents",
        "revert ReentrancyGuard()"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function create(address unitOwner, bytes32 unitHash, uint32[] memory dependencies)\n        external virtual returns (uint256 unitId)\n    {\n        // Reentrancy guard\n        if (_locked > 1) {\n            revert ReentrancyGuard();\n        }\n        _locked = 2;\n\n        // Check for the manager privilege for a unit creation\n        if (manager != msg.sender) {\n            revert ManagerOnly(msg.sender, manager);\n        }\n\n        // Checks for a non-zero owner address\n        if(unitOwner == address(0)) {\n            revert ZeroAddress();\n        }\n\n        // Check for the non-zero hash value\n        if (unitHash == 0) {\n            revert ZeroValue();\n        }\n        \n        // Check for dependencies validity: must be already allocated, must not repeat\n        unitId = totalSupply;\n        _checkDependencies(dependencies, uint32(unitId));\n\n        // Unit with Id = 0 is left empty not to do additional checks for the index zero\n        unitId++;\n\n        // Initialize the unit and mint its token\n        Unit storage unit = mapUnits[unitId];\n        unit.unitHash = unitHash;\n        unit.dependencies = dependencies;\n\n        // Update the map of subcomponents with calculated subcomponents for the new unit Id\n        // In order to get the correct set of subcomponents, we need to differentiate between the callers of this function\n        // Self contract (unit registry) can only call subcomponents calculation from the component level\n        uint32[] memory subComponentIds = _calculateSubComponents(UnitType.Component, dependencies);\n        // We need to add a current component Id to the set of subcomponents if the unit is a component\n        // For example, if component 3 (c3) has dependencies of [c1, c2], then the subcomponents will return [c1, c2].\n        // The resulting set will be [c1, c2, c3]. So we write into the map of component subcomponents: c3=>[c1, c2, c3].\n        // This is done such that the subcomponents start getting explored, and when the agent calls its subcomponents,\n        // it would have [c1, c2, c3] right away instead of adding c3 manually and then (for services) checking\n        // if another agent also has c3 as a component dependency. The latter will consume additional computation.\n        if (unitType == UnitType.Component) {\n            uint256 numSubComponents = subComponentIds.length;\n            uint32[] memory addSubComponentIds = new uint32[](numSubComponents + 1);\n            for (uint256 i = 0; i < numSubComponents; ++i) {\n                addSubComponentIds[i] = subComponentIds[i];\n            }\n            // Adding self component Id\n            addSubComponentIds[numSubComponents] = uint32(unitId);\n            subComponentIds = addSubComponentIds;\n        }\n        mapSubComponents[unitId] = subComponentIds;\n\n        // Set total supply to the unit Id number\n        totalSupply = unitId;\n        // Safe mint is needed since contracts can create units as well\n        _safeMint(unitOwner, unitId);\n\n        emit CreateUnit(unitId, unitType, unitHash);\n        _locked = 1;\n    }"
}