% 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/08/07} {0.4.1} {Jiazhu (Dual-column notes) support}

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

% Global Jiazhu Parameters (moved from vertical.sty)
% Jiazhu-specific keys set default values for textflow.
% Keys not defined here are forwarded to luatexcn/textflow via .unknown handler,
% so jiazhu automatically supports all textflow keys without manual sync.
\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},
    % Forward unknown keys to textflow (auto-balance, grid-height, etc.)
    unknown .code:n =
      {
        \keys_set:nx { luatexcn / textflow }
          { \l_keys_key_str = { \exp_not:n {#1} } }
      },
  }

\NewDocumentCommand{\jiazhuSetup}{ m }
  {
    \keys_set:nn { luatexcn / jiazhu } { #1 }
    % Sync jiazhu-specific defaults to textflow core layer
    \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 Shortcut (夹注捷径) - process_input_buffer based replacement
% ============================================================================
\lua_now:e { require("guji.luatex-cn-guji-jiazhu-shortcut") }

% \jiazhuShortcut[opts]{open}{close}
% Register a bracket pair as jiazhu shortcut.
% e.g. \jiazhuShortcut                          →  默认 【...】 becomes \夹注{...}
% e.g. \jiazhuShortcut{（}{）}                  →  （...） becomes \夹注{...}
% e.g. \jiazhuShortcut[font-color=red]{（}{）}  →  （...） becomes \夹注[font-color=red]{...}
\NewDocumentCommand{\jiazhuShortcut}{ O{} g g }
  {
    \IfNoValueTF { #2 }
      { \__luatexcn_jiazhu_shortcut_register:nnn { #1 } { 【 } { 】 } }
      {
        \IfNoValueTF { #3 }
          { \__luatexcn_jiazhu_shortcut_register:nnn { #1 } { #2 } { 】 } }
          { \__luatexcn_jiazhu_shortcut_register:nnn { #1 } { #2 } { #3 } }
      }
  }
\cs_new_protected:Nn \__luatexcn_jiazhu_shortcut_register:nnn
  {
    \lua_now:e
      {
        require("guji.luatex-cn-guji-jiazhu-shortcut").register(
          [[#2]],
          [[#3]],
          [[\c_backslash_str 夹注]],
          [[#1]]
        )
      }
  }

% 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}
\NewCommandCopy{\夹注捷径}{\jiazhuShortcut}
\NewEnvironmentCopy{夹注环境}{JiaZhuEnv}
% Traditional Chinese / 繁体
\NewCommandCopy{\夾注}{\JiaZhu}
\NewCommandCopy{\夾注設置}{\jiazhuSetup}
\NewCommandCopy{\夾注捷徑}{\jiazhuShortcut}
\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%
