<!--
// Copyright © 2022 Hardcore Engineering Inc.
// 
// Licensed under the Eclipse Public 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 https://www.eclipse.org/legal/epl-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.
-->
<script lang="ts">
  import { Ref, SortingOrder } from '@anticrm/core'
  import { getEmbeddedLabel, IntlString, translate } from '@anticrm/platform'
  import { createQuery } from '@anticrm/presentation'
  import { Sprint } from '@anticrm/tracker'
  import type { ButtonKind, ButtonSize } from '@anticrm/ui'
  import { Button, ButtonShape, eventToHTMLElement, SelectPopup, showPopup, Label } from '@anticrm/ui'
  import tracker from '../../plugin'
  import { sprintStatusAssets } from '../../types'

  export let value: Ref<Sprint> | null | undefined
  export let shouldShowLabel: boolean = true
  export let isEditable: boolean = true
  export let onSprintIdChange: ((newSprintId: Ref<Sprint> | undefined) => void) | undefined = undefined
  export let popupPlaceholder: IntlString = tracker.string.AddToSprint
  export let kind: ButtonKind = 'no-border'
  export let size: ButtonSize = 'small'
  export let shape: ButtonShape = undefined
  export let justify: 'left' | 'center' = 'center'
  export let width: string | undefined = 'min-content'
  export let onlyIcon: boolean = false
  export let enlargedText = false

  let selectedSprint: Sprint | undefined
  let defaultSprintLabel = ''

  const query = createQuery()
  let rawSprints: Sprint[] = []
  query.query(
    tracker.class.Sprint,
    {},
    (res) => {
      rawSprints = res
    },
    {
      sort: { startDate: SortingOrder.Descending }
    }
  )

  $: handleSelectedSprintIdUpdated(value, rawSprints)

  $: translate(tracker.string.Sprint, {}).then((result) => (defaultSprintLabel = result))
  const sprintIcon = tracker.icon.Sprint
  $: sprintText = shouldShowLabel ? selectedSprint?.label ?? defaultSprintLabel : undefined

  const handleSelectedSprintIdUpdated = async (newSprintId: Ref<Sprint> | null | undefined, sprints: Sprint[]) => {
    if (newSprintId === null || newSprintId === undefined) {
      selectedSprint = undefined

      return
    }

    selectedSprint = sprints.find((it) => it._id === newSprintId)
  }

  const handleSprintEditorOpened = async (event: MouseEvent): Promise<void> => {
    event.stopPropagation()
    if (!isEditable) {
      return
    }

    const sprintInfo = [
      { id: null, icon: tracker.icon.Sprint, label: tracker.string.NoSprint },
      ...rawSprints.map((p) => ({
        id: p._id,
        icon: tracker.icon.Sprint,
        text: p.label,
        category: sprintStatusAssets[p.status]
      }))
    ]

    showPopup(
      SelectPopup,
      { value: sprintInfo, placeholder: popupPlaceholder, searchable: true },
      eventToHTMLElement(event),
      onSprintIdChange
    )
  }
</script>

{#if onlyIcon || sprintText === undefined}
  <Button
    {kind}
    {size}
    {shape}
    {width}
    {justify}
    icon={sprintIcon}
    disabled={!isEditable}
    on:click={handleSprintEditorOpened}
  />
{:else}
  <Button
    {kind}
    {size}
    {shape}
    {width}
    {justify}
    icon={sprintIcon}
    disabled={!isEditable}
    on:click={handleSprintEditorOpened}
    ><svelte:fragment slot="content">
      <span class="{enlargedText ? 'ml-1 text-base fs-bold' : 'text-md'} overflow-label content-accent-color">
        <Label label={getEmbeddedLabel(sprintText)} />
      </span>
    </svelte:fragment></Button
  >
{/if}
