<?xml version="1.0"?>
<!--
  Copyright (C) 2014-2023 Ryan Specialty, LLC.

  This file is part of tame-core.

  tame-core is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
         xmlns:c="http://www.lovullo.com/calc"
         xmlns:t="http://www.lovullo.com/rater/apply-template"
         core="true"
         desc="Aliasing Values">

  These alias templates allow giving a new name to existing values.
  They are most useful for avoiding too many template-generated variables.

  If an alias name matches the name of the source,
    then no alias will be generated;
      this behavior is useful when generating aliases in templates where the
      caller may or may not choose a name that would otherwise conflict with
      its internal representation.

  If no description is provided,
    aliases will inherit the description from the source symbol.

  <template name="_classify-alias_"
            desc="Alias classification">
    <param name="@as@"   desc="Classification alias name" />
    <param name="@from@" desc="Source classification" />

    <param name="@desc@" desc="Classification alias description">
      <param-sym-value prefix=":class:" name="@from@" value="desc" />
    </param>

    <param name="@yields@" desc="Classification alias yield">
      <text></text>
    </param>

    <param name="@__src_yields@" desc="Source classification yield">
      <param-class-to-yields name="@from@" />
    </param>


    <unless name="@as@" eq="@from@">
      <classify as="@as@" desc="@desc@" yields="@yields@">
        <match on="@__src_yields@" />
      </classify>
    </unless>
  </template>


  <template name="_rate-alias_"
            desc="Alias calculation">
    <param name="@yields@" desc="Alias name" />
    <param name="@from@"   desc="Source name" />


    <unless name="@from@" eq="@yields@">
      <rate yields="@yields@">
        <c:value-of name="@from@" />
      </rate>
    </unless>
  </template>


  <template name="_rate-each-alias_"
            desc="Alias generator">
    <param name="@generates@" desc="Generator alias name" />
    <param name="@from@"      desc="Source generator" />

    <param name="@desc@" desc="Generator alias description">
      <param-sym-value name="@from@" value="desc" />
    </param>

    <param name="@yields@" desc="Yield alias name">
      <text>_</text>
      <param-value name="@generates@" />
    </param>


    <unless name="@generates@" eq="@from@">
      <rate yields="@yields@">
        <c:sum of="@from@" desc="@desc@" generates="@generates@" index="k">
          <c:value-of name="@from@" index="k" />
        </c:sum>
      </rate>
    </unless>
  </template>
</package>
