// @ts-ignore
import { OpenSheetMusicDisplay } from '/osmd-extended/src'
import detailState from '/src/pages/detail/state'
import event from '/src/components/music-score/event'

/**
 * 音符点击事件
 */
export const noteClickEvent = (evt: Event): void => {
  event.emit('container-click', evt)
  const path = (evt as any).path || (evt.composedPath && evt.composedPath())
  for (const el of path) {
    if (el.classList && el.classList.contains('vf-stavenote')) {
      event.emit('click', evt, el)
    }
  }
}

export const setEngravingRules = (osmd: OpenSheetMusicDisplay) => {
  // osmd.EngravingRules.UseJustifiedBuilder = false
  osmd.EngravingRules.CompactMode = true // 页面顶部底部间距
  // osmd.EngravingRules.DYSlursStartingAtSameStaffEntryYOffset = 2
  osmd.EngravingRules.DYMinNoteWidth = 3
  // osmd.EngravingRules.WedgePlacementBelowY = 4
  // osmd.EngravingRules.LyricUnderscoreLineWidth = 0.12 // 1------ 线粗细
  osmd.EngravingRules.VoiceSpacingMultiplierVexflow = 0.65
  osmd.EngravingRules.VoiceSpacingAddendVexflow = 2.0

  // tight rendering mode, lower margins and safety distances between systems, staffs etc. may cause overlap.
  // these options can afterwards be finetuned by setting osmd.rules.BetweenStaffDistance for example
  osmd.EngravingRules.MinSkyBottomDistBetweenStaves = 2.0 // default 1.0. this can cause collisions with slurs and dynamics sometimes
  osmd.EngravingRules.MinSkyBottomDistBetweenSystems = 2.0 // default 5.0
  // note that osmd.EngravingRules === osmd.rules, since it's passed as a reference

  osmd.EngravingRules.MinimumStaffLineDistance = 4.0
  osmd.EngravingRules.BetweenStaffDistance = 2.5
  osmd.EngravingRules.StaffDistance = 3.5
  osmd.EngravingRules.MinimumDistanceBetweenSystems = 1
  // osmd.EngravingRules.PageTopMargin = 0.0; // see osmd.EngravingRules.PageTopMarginNarrow used in compact mode
  osmd.EngravingRules.PageBottomMargin = 0.0
  osmd.EngravingRules.PageLeftMargin = 2.0
  osmd.EngravingRules.PageRightMargin = 2.0
  osmd.EngravingRules.PageTopMarginNarrow = 3.0
  osmd.EngravingRules.WedgeEndDistanceBetweenTimestampsFactor = 1
  osmd.EngravingRules.RehearsalMarkXOffset = -18
  osmd.EngravingRules.RehearsalMarkYOffset = 10

  osmd.EngravingRules.DYWorkId = detailState.activeDetail?.parentCategoriesId || 0
}
