Remove the text section/subsection before section/subsection number in table of contents

table of contents

I am preparing a thesis template. Therefore, I use many packages and codes. I need a centered section in the table of contents (see mainsection in the code) without page numbers. I have to write the first and the second at the beginning of the mainsections in Turkish. I managed to do this, but the writing in the table of contents of the first section/subsection/subsection/paragraph after each mainsection is incorrect. Moreover, dots after the first section/subsection/subsection/paragraph are not appearing/running. I also need the dots. Can you help me fix this? The MWE and their outputs are as follows:

\documentclass[a4paper,12pt]{article}
\renewcommand{\baselinestretch}{1.4}
\usepackage[left=3cm, top=3cm, right=2.5cm, bottom=2.5cm]{geometry}
\usepackage{mathptmx}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex
\usepackage{amsmath,amsthm,amssymb,enumerate,graphics,latexsym,fancyhdr,multicol,fleqn,multirow,tabu}
\usepackage{color,setspace,verbatim,graphicx,float,subcaption,sectsty,cite,sectsty,secdot,parskip,hanging,titletoc,indentfirst,afterpage} %,
\usepackage{tocloft}
\usepackage{url}
\usepackage{ltablex}
\usepackage{booktabs}
\usepackage[natbibapa]{apacite}
\usepackage{caption}
\usepackage{xstring}
\renewcommand\cftsecaftersnumb{\hfill}
\setlength{\cftbeforetoctitleskip}{50pt}
\setlength{\cftaftertoctitleskip}{3\baselineskip}
\dottedcontents{section}[0em]{}{2.9em}{0.25pc}

\dottedcontents{paragraph}[5.2em]{}{2.5em}{0.25pc}

\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
\renewcommand{\paragraph}{\thesubsection.\thesubsubsection\arabic{paragraph}.}

\newcommand{\numyazi}[1]{
    \IfEqCase{#1}{
        {1}{XXX}
        {2}{YYY}
        {3}{ZZZ}
    }[\PackageError{numyazi}{Undefined option to numyazi: #1}{}]
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{secnum}
\setcounter{section}{0}
\newcommand{\mainsection}[1]
{
\stepcounter{secnum}
\stepcounter{section}
        \section*{\numyazi{\arabic{secnum}} SECTION \\\vspace{0.2cm}#1}
        \cftaddtitleline{toc}{section}{\protect\numberline{}\newline\parbox{15.5cm}{\parbox{15.5cm}{\protect\centering \protect\numyazi{\arabic{secnum}} SECTION}\newline\parbox{15.5cm}{\protect\centering #1}}}\newline{}}

\usepackage{titlesec}
\titleformat{\section}{\centering\normalfont\fontsize{12}{12}\bfseries}{\thesection}{1em}{}
\titlespacing{\section}{0pt}{6pt}{18pt} % {sol boşluk}{üst boşluk}{alt boşluk}
\titleformat{\subsection}[block]{\normalfont\fontsize{12}{12}\bfseries}{\hspace{1.25cm}\thesubsection.}{3pt}{\hangindent=0cm} % \hangindent=1cm  \hangafter=-1
\titlespacing{\subsection}{0cm}{21pt}{18pt} % {sol boşluk}{üst boşluk}{alt boşluk}
\titleformat{\subsubsection}[block]{\normalfont\fontsize{12}{12}\bfseries}{\hspace{1.25cm}\thesubsubsection.}{3pt}{\hangindent=0cm} % \hangindent=1cm  \hangafter=-1
\titlespacing{\subsubsection}{0cm}{21pt}{18pt} % {sol boşluk}{üst boşluk}{alt boşluk}
\titleformat{\paragraph}[block]{\normalfont\fontsize{12}{12}\bfseries}{\hspace{1.25cm}\thesubsubsection.}{3pt}{\hangindent=0cm} % \hangindent=1cm  \hangafter=-1
\titlespacing{\paragraph}{0cm}{6cm}{*0} % {sol boşluk}{üst boşluk}{alt boşluk}
\cftsetindents{subsection}{0cm}{0.9\baselineskip}
\cftsetindents{subsubsection}{0.9cm}{1.3\baselineskip}
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
            {-2.5ex\@plus -8ex \@minus -.25ex}%
            {1.25ex \@plus .25ex}%
            {\normalfont\normalsize\bfseries}}
\makeatother
\begin{document}
\begin{onehalfspacing}
\tableofcontents
\end{onehalfspacing}
\newpage
\mainsection{FIRST MAIN SECTION TITLE}\setcounter{table}{0}
\subsection{First Subsection Title}
xxx
\subsection{Second Subsection Title}
xxx
\subsubsection{First Subsubsection Title}
xxx
\mainsection{SECOND MAIN SECTION TITLE}\setcounter{table}{0}
\subsubsection{First Subsubsection Title}
xxx
\subsection{First Subsection Title}
xxx
\end{document}

View of the table of contents

Text part

Best Answer

I solved the problem when I wrote the code \addtocontents{toc}{\protect\setcounter{}{}} after the \mainsection code, The table of contents was corrected.

\mainsection{FIRST MAIN SECTION TITLE}\addtocontents{toc}{\protect\setcounter{}{}}

\mainsection{SECOND MAIN SECTION TITLE}\addtocontents{toc}{\protect\setcounter{}{}}
Related Question