public function getDocumentState($document, ?int $assume = null)
    {
        $oid = spl_object_hash($document);

        if (isset($this->documentStates[$oid])) {
            return $this->documentStates[$oid];
        }

        if (null !== $assume) {
            return $assume;
        }

        // State here can only be NEW or DETACHED, as MANAGED and REMOVED states are known.
        $class = $this->manager->getClassMetadata(get_class($document));
        $id = $class->getSingleIdentifier($document);

        if (empty($id)) {
            return self::STATE_NEW;
        }

        if ($this->tryGetById($id, $class)) {
            return self::STATE_DETACHED;
        }

        $persister = $this->getDocumentPersister($class->name);
        if ($persister->exists(['_id' => $id])) {
            return self::STATE_DETACHED;
        }

        return self::STATE_NEW;
    }