<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT" xmlns:js="http://saxonica.com/ns/globalJS"
    xmlns:prop="http://saxonica.com/ns/html-property"
    xmlns:style="http://saxonica.com/ns/html-style-property"
    xmlns:o="http://piez.org/TEIoverlap/xslt/util" version="2.0"
    xpath-default-namespace="http://www.w3.org/1999/xhtml" exclude-result-prefixes="#all"
    extension-element-prefixes="ixsl">
    <!-- Saxon-CE stylesheet, to assist in working with html files derived from TAN data -->
    <xsl:variable name="sortable-classes" as="xs:string*" select="('qty', 'avg', 'stub')"/>
    <xsl:variable name="apos" select='"&apos;"'/>
    <xsl:template match="div[@class = 'table-head']">
        <xsl:result-document href="#feedback" method="ixsl:replace-content">
            <div id="feedback">You clicked the table head</div>
        </xsl:result-document>
    </xsl:template>
    <xsl:template
        match="div[(parent::div, parent::div/parent::div)[@class = 'table-head']][tokenize(@class, '\s+') = $sortable-classes]"
        mode="ixsl:onclick">
        <xsl:variable name="this-table" select="ancestor::div[@id = 'table']"/>
        <xsl:variable name="this-primary-col" select="count(parent::*/preceding-sibling::div) + 1"/>
        <xsl:variable name="this-secondary-col" select="count(preceding-sibling::div) + 1"/>
        <xsl:variable name="sort-should-be-descending" as="xs:boolean"
            select="
                if (tokenize(@class, '\s+') = 'asc') then
                    true()
                else
                    false()"/>
        <xsl:result-document href="#table" method="ixsl:replace-content">
            <!--<xsl:apply-templates select="$this-table/div[@class = 'table-head']"
                mode="reset-table-head">
                <xsl:with-param name="prim-col" select="$this-primary-col" tunnel="yes"/>
                <xsl:with-param name="sec-col" select="$this-secondary-col" tunnel="yes"/>
                <xsl:with-param name="sort" select="$sort-should-be-descending"/>
            </xsl:apply-templates>-->
            <xsl:copy-of select="$this-table/div[@class = 'table-head']"/>
            <xsl:copy-of select="$this-table/div[tokenize(@class, '\s+') = 'tr']"/>
            <!--<xsl:copy-of select="$this-table/*"/>-->
        </xsl:result-document>
        <xsl:result-document href="#feedback" method="ixsl:replace-content">
            <xsl:apply-templates select="." mode="print-element"></xsl:apply-templates>
            <!--<div id="feedback">
                <xsl:value-of select="$this-primary-col, $this-secondary-col"/>
            </div>-->
        </xsl:result-document>
    </xsl:template>

    <!-- DEFAULT FOR ALL NODES, SHALLOW-COPY -->
    <xsl:template match="node()" mode="reset-table-head print-element">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates mode="#current"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="div[tokenize(@class, '\s+') = $sortable-classes]" mode="reset-table-head">
        <xsl:param name="prim-col" as="xs:integer?" tunnel="yes"/>
        <xsl:param name="sec-col" as="xs:integer?" tunnel="yes"/>
        <xsl:variable name="this-primary-col" select="count(parent::*/preceding-sibling::div) + 1"/>
        <xsl:variable name="this-secondary-col" select="count(preceding-sibling::div) + 1"/>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="class">
                <xsl:choose>
                    <xsl:when
                        test="$this-primary-col = $prim-col and $this-secondary-col = $sec-col">
                        <xsl:choose>
                            <xsl:when test="matches(@class, 'asc')">
                                <xsl:value-of select="replace(@class, 'asc', 'desc')"/>
                            </xsl:when>
                            <xsl:when test="matches(@class, 'desc')">
                                <xsl:value-of select="replace(@class, 'desc', 'asc')"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="concat(@class, ' asc')"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="replace(@class, ' ?(asc|desc)', '')"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <!--p: <xsl:value-of select="$prim-col"/>
            s: <xsl:value-of select="$sec-col"/>
            this-p: <xsl:value-of select="$this-primary-col"/>
            this-s: <xsl:value-of select="$this-secondary-col"/>-->
            <!--<xsl:copy-of select="node()"/>-->
            <xsl:apply-templates mode="#current"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*" mode="print-element">
        <xsl:variable name="indent"
            select="
                for $i in (1 to count(ancestor::*))
                return
                    ' '"/>
        <xsl:value-of select="concat($indent, '&lt;', name()), ' '"/>
        <xsl:for-each select="@*">
            <xsl:value-of select="concat(name(), '=', $apos, ., $apos, ' ')"/>
        </xsl:for-each>
        <xsl:value-of select="'>&#xA;'"/>
        <xsl:apply-templates mode="#current"/>
        <xsl:value-of select="concat($indent, '&lt;/', name()), '>&#xA;'"/>
    </xsl:template>
</xsl:stylesheet>
