\ProvidesPackage{langsci-tbls}[2023/01/06 langsci-tbls]

\RequirePackage{etoolbox}
\newbool{langsci@tbls@tcolorbox}
\booltrue{langsci@tbls@tcolorbox}

\DeclareOption{mdframed}{\boolfalse{langsci@tbls@tcolorbox}}
\DeclareOption{tcolorbox}{\booltrue{langsci@tbls@tcolorbox}}

\ProcessOptions\relax

\ifbool{langsci@tbls@tcolorbox}
  {
    \PackageInfo{langsci-tbls}
    {
        langsci-tbls loaded with option `tcolorbox`.
    }
    \RequirePackage{tcolorbox}
    \tcbuselibrary{breakable, skins, xparse}
    \tcbset
      {
        skin = empty,
        width = \linewidth,
        breakable = true,
        fonttitle = \sffamily\bfseries\Large,
        coltitle = black,
        graphical environment = tikzpicture,
        sharp corners = all,
        no shadow,
        before upper = {\parindent=10.95pt\@afterheading\@afterindentfalse},
        before = {\par\vskip\baselineskip\pagebreak[0]},
        after = {\par}
      }
    \DeclareTColorBox{tblslineshorizontal}{m O{0.8mm} O{\lsSeriesColor}}
      {
        graphical environment = tikzpicture,
        title = {#1},
        boxsep = 0pt,
        toptitle = 5mm,
        top = 5mm,
        bottom = 5mm,
        left = 0mm,
        right = 0mm,
        borderline horizontal = {#2}{0pt}{#3},
        beforeafter skip balanced = \baselineskip,
        subtitle style = 
          {
            top = 3.25ex,
            boxrule = 0pt,
            colback = white,
            enhanced,
            interior hidden,
            frame hidden,
            fontupper = \normalfont\sffamily\bfseries\large
          } 
      }
    \DeclareTColorBox{tblsfilled}{m O{black!12}}
      {
        graphical environment = tikzpicture,
        title = {#1},
        boxsep = 0pt,
        toptitle = 5mm,
        top = 5mm,
        bottom = 5mm,
        left = 5mm,
        right = 5mm,
        frame engine = path,
        frame style = {fill=#2},
        sharp corners = all,
        subtitle style = 
          {
            top = 3.25ex,
            boxrule = 0pt,
            colback = #2,
            enhanced,
            interior hidden,
            frame hidden,
            fontupper = \normalfont\sffamily\bfseries\large
          } 
      }
    \DeclareTColorBox{tblsframed}{m O{0.8mm} O{\lsSeriesColor}}
      {
        graphical environment = tikzpicture,
        title = {#1},
        boxsep = 0pt,
        toptitle = 5mm,
        top = 5mm,
        bottom = 5mm,
        left = 5mm,
        right = 5mm,
        borderline = {#2}{0pt}{#3},
        beforeafter skip balanced = \baselineskip,
        subtitle style = 
          {
            top = 3.25ex,
            boxrule = 0pt,
            colback = white,
            enhanced,
            interior hidden,
            frame hidden,
            fontupper = \normalfont\sffamily\bfseries\large
          } 
      }
    \DeclareTColorBox{tblsfilledsymbol}{m O{black!12} m}
      {
        skin = enhanced,
        graphical environment = tikzpicture,
        title = {#1},
        boxsep = 0pt,
        toptitle = 10mm,
        top = 5mm,
        bottom = 5mm,
        left = 5mm,
        right = 5mm,
        frame style = {fill=#2},
        interior hidden,
        sharp corners = all,
        enlarge top initially by = 5mm,
        underlay unbroken and first = 
          {
            \node 
              [
                anchor = base,
                xshift = 11.3mm,
                yshift = -5mm,
                fill = white,
                draw = #2,
                inner sep = 1mm,
                line width = 0.8mm
              ] at (frame.north west) 
              {{\color{#2}\includegraphics[width=10mm]{tbls-#3.pdf}}};
          },
        subtitle style = 
          {
            top = 3.25ex,
            boxrule = 0pt,
            colback = #2,
            enhanced,
            interior hidden,
            frame hidden,
            fontupper = \normalfont\sffamily\bfseries\large
          } 
      }
    \DeclareTColorBox{tblsframedsymbol}{m O{\lsSeriesColor} O{0.8mm} m}
      {
        graphical environment = tikzpicture,
        title = {#1},
        boxsep = 0pt,
        toptitle = 10mm,
        top = 5mm,
        bottom = 5mm,
        left = 5mm,
        right = 5mm,
        borderline = {#3}{0pt}{#2},
        interior hidden,
        enlarge top initially by = 5mm,
        underlay unbroken and first = 
          {
            \node 
              [
                anchor = base,
                xshift = 11.3mm,
                inner sep = 1mm,
                yshift = -5mm,
                fill = white,
                draw = #2,
                line width = #3
              ] at (frame.north west) 
              {{\color{#2}\includegraphics[width=10mm]{tbls-#4.pdf}}};
          },
        subtitle style = 
          {
            top = 3.25ex,
            boxrule = 0pt,
            colback = white,
            enhanced,
            interior hidden,
            frame hidden,
            fontupper = \normalfont\sffamily\bfseries\large
          } 
      }
  }

\notbool{langsci@tbls@tcolorbox}
  {
    \PackageInfo{langsci-tbls}
    {
        langsci-tbls loaded with option `mdframed`.
    }
    \usepackage[framemethod=tikz]{mdframed}
    \mdfsetup
      {
        skipabove=2\baselineskip,
        skipbelow=2\baselineskip,
        frametitlefont=\sffamily\bfseries\Large, 
        needspace=4\baselineskip, 
        splittopskip=\baselineskip,
        apptotikzsetting={\tikzset{mdfbackground/.append style={draw=none}}}
      }
    
    \AtEndPreamble{% Check if the external library from tikz has been loaded in the preamble. If so, skip externalising for all mdframed boxes.
        \ifcsname tikz@library@external@loaded\endcsname
            \mdfsetup{settings={\tikzexternaldisable},endcode={\tikzexternalenable}}
        \fi
    }
    
    \newcommand{\langscisymbol}{book}
    \newlength{\trennlinie}
    \setlength{\trennlinie}{.8mm}
    \newcommand{\tblsboxcolor}{black}
    \newcommand{\tblslinecolour}{lsYellow}
    
    \mdfdefinestyle{yellowexercise}{%
        everyline=true,
        ignorelastdescenders=true,
        middlelinewidth=1.5pt,
        linecolor=\tblslinecolour,
        outerlinewidth=5mm,
        innerleftmargin=5mm, 
        innerrightmargin=5mm, 
        innerbottommargin=5mm, 
        innertopmargin=5mm,
        frametitleaboveskip=10mm, 
        frametitlebelowskip=0mm,
        frametitlerule=false, 
        repeatframetitle=false,
        firstextra={\node[xshift=5mm,right,draw=\tblslinecolour, line width=1.5pt,rectangle, minimum size=10mm, fill=white] at (P-|O) {\color{\tblslinecolour}\includegraphics[width=10mm]{tbls-\langscisymbol.pdf}};}, % if environment is on multiple pages, print this on the first page.
        singleextra={\node[xshift=5mm,right,draw=\tblslinecolour, line width=1.5pt,rectangle, minimum size=10mm, fill=white] at (P-|O) {\color{\tblslinecolour}\includegraphics[width=10mm]{tbls-\langscisymbol.pdf}};} % if env. does not span multiple pages, print the same.
    }
    
    \mdfdefinestyle{greyexercise}{%
        ignorelastdescenders=true,
        middlelinewidth=0pt,
        linecolor=white,
        outerlinewidth=5mm,
        leftline=false,
        rightline=false,
        bottomline=false,
        backgroundcolor=\tblsboxcolor,
        innerleftmargin=5mm, 
        innerrightmargin=5mm, 
        innerbottommargin=5mm, 
        innertopmargin=5mm,
        frametitleaboveskip=10mm, 
        frametitlebelowskip=0mm,
        frametitlerule=false, 
        repeatframetitle=false,
        firstextra={\node[xshift=5mm,right,draw=\tblsboxcolor, line width=1.5pt,rectangle, minimum size=10mm, fill=white] at (P-|O) {\color{\tblsboxcolor}\includegraphics[width=10mm]{tbls-\langscisymbol.pdf}};},
        singleextra={\node[xshift=5mm,right,draw=\tblsboxcolor, line width=1.5pt,rectangle, minimum size=10mm, fill=white] at (P-|O) {\color{\tblsboxcolor}\includegraphics[width=10mm]{tbls-\langscisymbol.pdf}};}
    }
    
    
    \mdfdefinestyle{tblsbox}{%
    	leftline=false, 
    	rightline=false, 
    	frametitleaboveskip=5mm, 
    	frametitlebelowskip=0mm,
    	frametitlerule=false, 
    	repeatframetitle=false, 
    	linewidth=\trennlinie, 
    	linecolor=\tblslinecolour, 
    	innerbottommargin=5mm, 
    	innertopmargin=5mm,
    	innerleftmargin=0mm,
    	innerrightmargin=0mm
    	}
    
    \mdfdefinestyle{tblsframedbox}{%
    	frametitleaboveskip=5mm, 
    	frametitlebelowskip=0mm,
    	frametitlerule=false, 
    	repeatframetitle=false, 
    	linewidth=\trennlinie, 
    	linecolor=\tblslinecolour, 
    	innerbottommargin=5mm, 
    	innertopmargin=5mm
    }
    
    \newcommand{\tblsfillcolour}{black!12}
    
    \mdfdefinestyle{tblsfilledbox}{%
    	hidealllines=true, 
    	backgroundcolor=\tblsfillcolour, 
    	frametitleaboveskip=5mm, 
    	frametitlebelowskip=0mm,
    	frametitlerule=false, 
    	repeatframetitle=false, 
    	innerleftmargin=5mm, 
    	innerrightmargin=5mm, 
    	innerbottommargin=5mm, 
    	innertopmargin=5mm
    }
    
    \mdfdefinestyle{tblsfilledcolumnbox}{% % Copy of tblsfilledbox with multicols
    	hidealllines=true, 
    	backgroundcolor=\tblsfillcolour, 
    	frametitleaboveskip=5mm, 
    	frametitlebelowskip=0mm,
    	frametitlerule=false, 
    	repeatframetitle=false, 
    	innerleftmargin=5mm, 
    	innerrightmargin=5mm, 
    	innerbottommargin=5mm, 
    	innertopmargin=5mm,
    	startinnercode={\begin{multicols}{2}},
    	endinnercode={\end{multicols}}
    }
    
    \mdfdefinestyle{tblsframedcolumnbox}{%
    	frametitleaboveskip=10mm, 
    	frametitlebelowskip=0mm,
    	frametitlerule=false, 
    	repeatframetitle=false, 
    	linewidth=\trennlinie, 
    	linecolor=\tblsboxcolor, 
    	innerbottommargin=5mm, 
    	innertopmargin=5mm, 
    	startinnercode={\begin{multicols}{2}},
    	endinnercode={\end{multicols}}
    }
    
    %%% MDFRAMED DOCUMENT COMMANDS %%%
    
    \newcommand{\tblssy}[4][black!12]{% Use like this \tblsfr[colour]{book|bulb|glass|law}{title}{text}, where [colour] is optional
      \renewcommand{\langscisymbol}{#2}\renewcommand{\tblsboxcolor}{#1}
      \begin{mdframed}[style=greyexercise,frametitle={#3}]
        #4
      \end{mdframed}
    }
    \newcommand{\tblsli}[4][black!20]{% Use like this \tblsli[colour]{line width}{title}{text}
    \renewcommand{\tblsboxcolor}{#1}\setlength{\trennlinie}{#2mm}
    \begin{mdframed}[style=tblsbox,frametitle={#3}]
    #4
    \end{mdframed}
    }
    \newcommand{\tblsfi}[3][black!12]{% Use like this \tblsfi[colour]{title}{text}, where [colour] is optional
    \renewcommand{\tblsfillcolour}{#1}
    \begin{mdframed}[style=tblsfilledbox,frametitle={#2}]
    #3
    \end{mdframed}
    }
    \newcommand{\tblsfr}[4][\lsSeriesColor]{% Use like this \tblsfr[colour]{book|bulb|glass|law}{title}{text}, where [colour] is optional
    \renewcommand{\tblslinecolour}{#1}\renewcommand{\langscisymbol}{#2}
    \begin{mdframed}[style=tblsbox,frametitle={#3}]
    #4
    \end{mdframed}
    }
    \newcommand{\tblsfd}[4]{ % Use like this: \tblsfd{colour}{line width}{title}{content}
    \renewcommand{\tblslinecolour}{#1}\setlength{\trennlinie}{#2mm}
    \begin{mdframed}[style=tblsframedbox,frametitle={#3}]
    #4
    \end{mdframed}
    }
  }{}
