#
# Commands covered: view -activetab, arrayDeleteSheet, presetsheets
#
# Regression tests for ticket 3933595861: deleting a sheet from a
# workbook read with xl_read and writing it back via presetstyles/
# presetsheets produced a file with an out of range activeTab, which
# Excel treats as a corrupted file. Also covers the hyperlink transfer
# of presetsheets, which errored out on a wrong dict key.
#

#
# (c) 2026 Alexander Schoepe
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#

if {[lsearch [namespace children] ::tcltest] == -1} {
  package require tcltest 2
  namespace import -force ::tcltest::*
  loadTestedCommands
}


package require ooxml


# Create a workbook with the three worksheets Alpha, Beta and Gamma,
# a hyperlink cell on every sheet and the given active tab.

proc makeViewTestWorkbook {file activetab} {
  set spreadsheet [::ooxml::xl_write new -creator {Tcl Test}]
  foreach name {Alpha Beta Gamma} {
    set sheet [$spreadsheet worksheet $name]
    $spreadsheet row $sheet
    $spreadsheet cell $sheet "Head $name" -string
    $spreadsheet cell $sheet 42
    $spreadsheet row $sheet
    $spreadsheet cell $sheet link -string -hyperlink https://example.com/ -tooltip tip
  }
  $spreadsheet view -activetab $activetab
  $spreadsheet write $file
  $spreadsheet destroy
}


test View-1.1 {view -activetab is written and read back} {
  makeViewTestWorkbook view-1.xlsx 2
  array set wb [::ooxml::xl_read view-1.xlsx]
  set result [dict get $wb(view) activetab]
  array unset wb
  set result
} 2

test View-1.2 {out of range activetab is clamped to 0 on write} {
  set spreadsheet [::ooxml::xl_write new]
  $spreadsheet worksheet {Sheet 1}
  $spreadsheet view -activetab 99
  $spreadsheet write view-2.xlsx
  $spreadsheet destroy
  array set wb [::ooxml::xl_read view-2.xlsx]
  set result [dict get $wb(view) activetab]
  array unset wb
  set result
} 0

test View-2.1 {arrayDeleteSheet shifts the active tab} {
  makeViewTestWorkbook view-1.xlsx 2
  array set wb [::ooxml::xl_read view-1.xlsx]
  ::ooxml::arrayDeleteSheet wb 0
  set result [dict get $wb(view) activetab]
  array unset wb
  set result
} 1

test View-2.2 {arrayDeleteSheet clamps the active tab if the active sheet is removed} {
  makeViewTestWorkbook view-1.xlsx 2
  array set wb [::ooxml::xl_read view-1.xlsx]
  ::ooxml::arrayDeleteSheet wb 2
  set result [dict get $wb(view) activetab]
  array unset wb
  set result
} 1

test View-3.1 {ticket 3933595861: delete a sheet, preset and write a valid file} {
  makeViewTestWorkbook view-1.xlsx 2
  array set wb [::ooxml::xl_read view-1.xlsx]
  ::ooxml::arrayDeleteSheet wb 0
  set spreadsheet [::ooxml::xl_write new]
  $spreadsheet presetstyles wb
  $spreadsheet presetsheets wb
  $spreadsheet write view-3.xlsx
  $spreadsheet destroy
  array unset wb
  # The written file must reference an existing sheet as active tab
  # and contain the two remaining sheets in the original order.
  array set wb [::ooxml::xl_read view-3.xlsx]
  set result [list [llength $wb(sheets)] $wb(0,n) $wb(1,n) [dict get $wb(view) activetab]]
  array unset wb
  set result
} {2 Beta Gamma 1}

test Preset-1.1 {presetsheets transfers hyperlinks and tooltips} {
  makeViewTestWorkbook view-1.xlsx 0
  array set wb [::ooxml::xl_read view-1.xlsx]
  set spreadsheet [::ooxml::xl_write new]
  $spreadsheet presetstyles wb
  $spreadsheet presetsheets wb
  $spreadsheet write view-4.xlsx
  $spreadsheet destroy
  array unset wb
  array set wb [::ooxml::xl_read view-4.xlsx]
  set result $wb(0,l,1,0)
  array unset wb
  set result
} {l https://example.com/ t tip}

file delete -force view-1.xlsx view-2.xlsx view-3.xlsx view-4.xlsx


# cleanup
::tcltest::cleanupTests
return
