#import "@preview/lemmify:0.1.5": *
#import "split-box.typ" : *

#set heading(numbering: "1.1")

#let cyrsmallcaps(body) = [
  #show regex("[а-яё]") : it => text(size:.7em, upper(it))
  #body
]

#let map-values(f, m) = {
  let res = (_nothing: none)
  res.remove("_nothing")
  for k in m.keys() {
    res.insert(k, f(m.at(k)))
  }
  res
}

#translations.insert("ru", map-values(cyrsmallcaps, (
    "theorem": "Теорема",
    "lemma": "Лемма",
    "corollary": "Следствие",
    "remark": "Замечание",
    "proposition": "Предположение",
    "example": "Пример",
    "definition": "Определение",
    
    "memorizer": "Напоминалочка",
    "property": "Свойство",
    "statement": "Условие",

    "proof": "Доказательство",
    "breakable-proof": "Доказательство",
)))

#let right-default-theorems(
  group,
  lang: "en",
  thm-styling: thm-style-simple,
  proof-styling: thm-style-proof,
  thm-numbering: thm-numbering-linear,
  ref-styling: thm-ref-style-simple,
  max-reset-level: 1
) = {
  let (proof, ..subgroup-map) = translations.at(lang)

  let (rules: rules-theorems, ..theorems) = new-theorems(
    group,
    subgroup-map,
    thm-styling: thm-styling,
    thm-numbering: thm-numbering
  )

  let (rules: rules-proof, proof) = new-theorems(
    group,
    (proof: translations.at(lang).at("proof")),
    thm-styling: proof-styling,
    thm-numbering: thm-numbering-proof,
    ref-numbering: thm-numbering
  )

  return (
    ..theorems,
    proof: 
      // use-proof-numbering(proof)
      proof
      ,
    // breakable-proof: translations.at(lang).at("breakable-proof"),
    rules: concat-fold((
      thm-reset-counter-heading.with(group, max-reset-level),
      rules-theorems,
      rules-proof
    ))
  )
}

#let (
  definition, theorem, lemma, corollary, remark, proposition, example, memorizer, property, statement, proof, rules: thm-rules
) = right-default-theorems("thm-group", lang: "ru", max-reset-level: 1)

#let (
  proof: breakable-proof, rules: bthm-rules
) = right-default-theorems("bthm-group", lang: "ru", max-reset-level: 1)
// #let (
//   definition, theorem, lemma, corollary, remark, proposition, example, proof, rules: thm-rules
// ) = default-theorems("thm-group", lang: "ru", max-reset-level: 1)



// #let (property, memorizer, rules: custom-rules) = new-theorems("thm-group", (
//   property: text(red)[Note],
//   memorizer: [Напоминалочка],
// ))
// #show: custom-rules


#import "@preview/rose-pine:0.1.0": apply, rose-pine-dawn, rose-pine-moon, rose-pine, apply-theme


#show: apply(variant: "rose-pine-moon")

#let rose-pine-theorem-colors(theme) = {
  (
    definition: theme.gold,
    theorem: theme.love,
    lemma: theme.love,
    corollary: theme.pine,
    
    remark: theme.foam,
    proof: theme.iris
  )
}

#let add-paint-if-some(res, source, key) = {
  if key in source {
    res.insert("paint", source.at(key))
  }

  res
}


#let my-theorem-styles = (thm-styles: (_nothing: none)) => body => {
  show: thm-rules
  show: bthm-rules
  let proof-border = (
    // The starting and ending lines
    above: none,
    below: none,
    // Lines to put between the box over multiple pages
    btwn-above: line(length: 100%, stroke: (dash:"dotted")),
    btwn-below: line(length: 100%, stroke: (dash:"dotted")),
    // Left/right lines
    // These *must* use `grid.vline()`, otherwise you will get an error.
    // To remove the lines, set them to: `grid.vline(stroke: none)`.
    // You could probably configure this better with a rowspan, but I'm lazy.
    left: grid.vline(stroke: add-paint-if-some((thickness: 1pt, dash: "dotted"), thm-styles, "proof")),
    right: grid.vline(stroke: none),
  )


  // show: it => thm-reset-counter-heading-at("thm-group", 1, it)

  show thm-selector("thm-group", subgroup: "definition"): it => box(
    it,
    stroke: (left: add-paint-if-some((thickness: 2pt), thm-styles, "definition")),
    inset: 1em,
  )
  show thm-selector("thm-group", subgroup: "theorem"): it => box(
    it,
    stroke: add-paint-if-some((thickness: 1pt), thm-styles, "theorem"),
    inset: 1em
  )
  show thm-selector("thm-group", subgroup: "lemma"): it => box(
    it,
    stroke: add-paint-if-some((thickness: 1pt, dash: "dotted"), thm-styles, "lemma"),
    inset: 1em
  )
  show thm-selector("thm-group", subgroup: "corollary"): it => box(
    it,
    stroke: add-paint-if-some((thickness: 1pt), thm-styles, "corollary"),
    inset: 1em
  )
  show thm-selector("thm-group", subgroup: "example"): it => box(
    it,
    inset: (left: 1em, right: 1em, top: 1em, bottom: 1em),
  )
  show thm-selector("thm-group", subgroup: "remark"): it => box(
    it,
    inset: (left: 1em, right: 1em, top: 1em, bottom: 1em),
  )
  let proof-with(element, it) = {
    element(
      it,
      stroke: (left:
        add-paint-if-some((thickness: 1pt, dash: "dotted"), thm-styles, "proof")
      ),
      inset: (left: 1em, right: 1em, top: 0.5em, bottom: 0.5em),
    )
    }

  show thm-selector("bthm-group", subgroup: "proof"): it => if (false) {
    split-box(it, border: proof-border)
  } else { proof-with(block.with(breakable: true), it) }

  show thm-selector("thm-group", subgroup: "proof"): it => proof-with(box, it)

  body
}

= Тест

#show thm-selector("thm-group"): my-theorem-styles(
  thm-styles: rose-pine-theorem-colors(rose-pine-moon)
  )

#show thm-selector("bthm-group"): my-theorem-styles(
  thm-styles: rose-pine-theorem-colors(rose-pine-moon)
  )

#theorem(name: "Some theorem")[
  Theorem content goes here.
]<thm-label-example>

#proof[
  That's quite obvious
  
  #remark[
    See YouTube lectures for deeper understanding
  ]

  #lemma[
    sdfa
  ]
  #proof[
    Lemma's proof
  ]
]<proof-label-example>


@proof-label-example and @thm-label-example[theorem]

#breakable-proof[
  #lorem(1000)
]
