% Copyright 2026 Open-Guji (https://github.com/open-guji)
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
% luatex-cn-guji-jiazhu.sty
% Jiazhu (夹注) support for vertical typesetting
% This is a subpackage of luatex_cn
%
\RequirePackage{expl3}
\RequirePackage{xparse}
\ProvidesExplPackage {guji/luatex-cn-guji-jiazhu} {2026/02/18} {0.3.0} {Jiazhu (Dual-column notes) support}

% Load the core textflow engine
\RequirePackage{core/luatex-cn-core-textflow}

% Global Jiazhu Parameters (moved from vertical.sty)
\tl_new:N \l__luatexcn_jiazhu_size_tl
\tl_new:N \l__luatexcn_jiazhu_font_tl
\tl_new:N \l__luatexcn_jiazhu_font_color_tl
\tl_new:N \l__luatexcn_jiazhu_align_tl

\keys_define:nn { luatexcn / jiazhu }
  {
    font-size .tl_set:N = \l__luatexcn_jiazhu_size_tl,
    font-size .initial:n = {},
    font .tl_set:N = \l__luatexcn_jiazhu_font_tl,
    font .initial:n = {},
    font-color .tl_set:N = \l__luatexcn_jiazhu_font_color_tl,
    font-color .initial:n = {},
    align .tl_set:N = \l__luatexcn_jiazhu_align_tl,
    align .initial:n = {outward},
  }

\NewDocumentCommand{\jiazhuSetup}{ m }
  {
    \keys_set:nn { luatexcn / jiazhu } { #1 }
    % Set core layer defaults (proper dependency direction: guji -> core)
    \tl_set_eq:NN \l__luatexcn_textflow_default_size_tl \l__luatexcn_jiazhu_size_tl
    \tl_set_eq:NN \l__luatexcn_textflow_default_font_tl \l__luatexcn_jiazhu_font_tl
    \tl_set_eq:NN \l__luatexcn_textflow_default_color_tl \l__luatexcn_jiazhu_font_color_tl
    \tl_set_eq:NN \l__luatexcn_textflow_default_align_tl \l__luatexcn_jiazhu_align_tl
  }

% Jiazhu Command (双行小注/夹注)
% We define \jiazhu as the primary command name here, delegating to \TextFlow
\NewDocumentCommand{\JiaZhu}{ O{} +m }
  {
    \TextFlow[#1]{#2}
  }

% Jiazhu Environment - Allows command expansion (e.g., \平抬)
% Same as \JiaZhu but uses environment form to allow content expansion
\NewDocumentEnvironment{JiaZhuEnv}{ O{} }
  {
    \TextFlowEnv[#1]
  }
  {
    \endTextFlowEnv
  }

\ExplSyntaxOff%

% ============================================================================
% CJK Aliases (中文别名)
% ============================================================================
% Simplified Chinese / 简体
\NewCommandCopy{\夹注}{\JiaZhu}
\NewCommandCopy{\夹注设置}{\jiazhuSetup}
\NewCommandCopy{\单行夹注}{\DanHangJiaZhu}
\NewEnvironmentCopy{夹注环境}{JiaZhuEnv}
% Traditional Chinese / 繁体
\NewCommandCopy{\夾注}{\JiaZhu}
\NewCommandCopy{\夾注設置}{\jiazhuSetup}
\NewEnvironmentCopy{夾注環境}{JiaZhuEnv}
\NewCommandCopy{\單行夾注}{\DanHangJiaZhu}

% ============================================================
% Chinese key aliases / 中文 Key 别名
% ============================================================
\ExplSyntaxOn
\keys_define:nn { luatexcn / jiazhu }
  {
    % 简体
    字号 .tl_set:N = \l__luatexcn_jiazhu_size_tl,
    字体 .tl_set:N = \l__luatexcn_jiazhu_font_tl,
    字体颜色 .tl_set:N = \l__luatexcn_jiazhu_font_color_tl,
    对齐 .tl_set:N = \l__luatexcn_jiazhu_align_tl,
    % 繁体（与简体不同形的）
    字號 .tl_set:N = \l__luatexcn_jiazhu_size_tl,
    字體 .tl_set:N = \l__luatexcn_jiazhu_font_tl,
    字體顏色 .tl_set:N = \l__luatexcn_jiazhu_font_color_tl,
    對齊 .tl_set:N = \l__luatexcn_jiazhu_align_tl,
  }
\ExplSyntaxOff

\endinput%
