<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:tan="tag:textalign.net,2015:ns" xmlns="tag:textalign.net,2015:ns"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
   exclude-result-prefixes="#all" version="2.0">
   <!-- Core variables, parameters, and functions for any stylesheet that generates a TAN file. The primary purpose is to credit/blame the master stylsheet by means of <agent>, add a <role> if one doesn't exist, and log the change in <change> -->
   <!-- This stylesheet is meant to be imported (not included) by other master stylesheets. Any stylesheets that include it must have certain variables defined. -->

   <xsl:function name="tan:preceding-text-node" as="text()?">
      <!-- Input: any element -->
      <!-- Output: the text node, if any, that comes before it. -->
      <!-- This function is useful for replicating indentation levels. -->
      <xsl:param name="element" as="element()"/>
      <xsl:value-of select="($element/preceding::node()[1])[self::text()]"/>
   </xsl:function>
   
   <xsl:param name="stylesheet-iri" as="xs:string" required="yes"/>
   <xsl:param name="change-element" as="element(tan:change)+" required="yes"/>

   <xsl:variable name="agent-element-for-this-stylesheet"
      select="$self-resolved/*/tan:head/tan:agent[tan:IRI = $stylesheet-iri]"/>

   <xsl:variable name="stylesheet-id" as="xs:string">
      <xsl:choose>
         <xsl:when test="exists($agent-element-for-this-stylesheet)">
            <xsl:value-of select="$agent-element-for-this-stylesheet/@xml:id"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:variable name="new-id-number-start"
               select="
                  max((0,
                  for $i in $self-resolved/tan:TAN-A-div/tan:head/tan:agent/@xml:id[matches(., '^xslt\d+$')]
                  return
                     number(replace($i, '\D+', ''))))"/>
            <xsl:value-of select="concat('xslt', string($new-id-number-start + 1))"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>
   
   <xsl:variable name="stylesheet-role"
      select="$TAN-keyword-files/tan:TAN-key/tan:body[@affects-element = 'role']/tan:item[tan:name = 'stylesheet']"/>
   <xsl:variable name="role-element-for-stylesheet"
      select="$self-resolved/*/tan:head/tan:role[tan:IRI = $stylesheet-role/tan:IRI]"/>
   <xsl:variable name="stylesheet-role-id">
      <xsl:choose>
         <xsl:when test="exists($role-element-for-stylesheet)">
            <xsl:value-of select="$role-element-for-stylesheet/@xml:id"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:variable name="new-id-number-start"
               select="
                  max((0,
                  for $i in $self-resolved/tan:TAN-A-div/tan:head/tan:role/@xml:id[matches(., '^stylesheet\d+$')]
                  return
                     number(replace($i, '\D+', ''))))"/>
            <xsl:value-of select="concat('stylesheet', string($new-id-number-start + 1))"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>

   <xsl:template match="comment() | processing-instruction() | text()" mode="credit-stylesheet">
      <xsl:copy-of select="."/>
   </xsl:template>
   <xsl:template match="*" mode="credit-stylesheet">
      <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates mode="#current"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="tan:agent" mode="credit-stylesheet">
      <xsl:copy-of select="."/>
      <xsl:if
         test="not(exists(following-sibling::tan:agent)) and not(exists($agent-element-for-this-stylesheet))">
         <xsl:value-of select="tan:preceding-text-node(.)"/>
         <agent xml:id="{$stylesheet-id}" roles="{$stylesheet-role-id}">
            <IRI>
               <xsl:value-of select="$stylesheet-iri"/>
            </IRI>
            <name>Stylesheet to populate a TAN-A-div file from collections.</name>
            <desc>Stylesheet at: <xsl:value-of
                  select="tan:uri-relative-to($this-stylesheet-uri, $doc-uri)"/></desc>
         </agent>
      </xsl:if>
   </xsl:template>
   <xsl:template match="tan:role" mode="credit-stylesheet">
      <xsl:copy-of select="."/>
      <xsl:if
         test="not(exists(following-sibling::tan:role)) and not(exists($role-element-for-stylesheet))">
         <xsl:value-of select="tan:preceding-text-node(.)"/>
         <role xml:id="{$stylesheet-role-id}" which="stylesheet"/>
      </xsl:if>
   </xsl:template>
   <xsl:template match="tan:change" mode="credit-stylesheet">
      <xsl:copy-of select="."/>
      <xsl:if test="not(exists(following-sibling::tan:change))">
         <xsl:value-of select="tan:preceding-text-node(.)"/>
         <xsl:copy-of select="$change-element"/>
      </xsl:if>
   </xsl:template>

</xsl:stylesheet>
