Theorem name and number

I want a code that can provide a link (ref) to a theorem with text which follows these conditions:

1.) If the theorem have a name (like \begin{theorem}[name]\end{theprem}), the text will be name Theorem.

2.) Otherwise, the text will be Theorem 1.1. (the number of the theorem).

I will be very gladful if someone can write me that code. Thank you.

Here is a way (as far as I understood the question):

\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}
\def\thname{name}
\newtheorem{theorem}{Theorem}[section]%added section after edit and picture
\newtheorem{mytheorem}{\thname{} Theorem}
\newenvironment{mtheorem}[1][name]{\def\thname{#1}\mytheorem}{\endmytheorem}
\def\themytheorem{:}

\begin{document}
\section{Test Section}
\begin{theorem}
\lipsum[1]
\end{theorem}
\begin{mtheorem}[Some]
\lipsum[1]
\end{mtheorem}
\begin{mtheorem}[Some Other]
\lipsum[1]
\end{mtheorem}
\begin{theorem}
\lipsum[1]
\end{theorem}

\end{document}


• Edited the code but not the screenshot in order to have theorem within section... ie 1.1, 1.2 etc – koleygr Aug 11 '19 at 1:27