<template>
  <div class="dropdown">
    <button :class="Object.assign({'btn': true, 'btn-outline-primary': true, 'dropdown-toggle': true, small: props.small})" type="button" data-bs-toggle="dropdown" aria-expanded="false">
      Actions
    </button>
    <ul class="dropdown-menu">
      <li v-if="props.canEdit && is_extension_editable(props.storedObject.type)">
        <wopi-edit-button :stored-object="props.storedObject" :classes="{'dropdown-item': true}" :execute-before-leave="props.executeBeforeLeave"></wopi-edit-button>
      </li>
      <li v-if="props.storedObject.type != 'application/pdf' && is_extension_viewable(props.storedObject.type) && props.canConvertPdf">
        <convert-button :stored-object="props.storedObject" :filename="filename" :classes="{'dropdown-item': true}"></convert-button>
      </li>
      <li v-if="props.canDownload">
        <download-button :stored-object="props.storedObject" :filename="filename" :classes="{'dropdown-item': true}"></download-button>
      </li>
    </ul>
  </div>

</template>

<script lang="ts" setup>

import ConvertButton from "./StoredObjectButton/ConvertButton.vue";
import DownloadButton from "./StoredObjectButton/DownloadButton.vue";
import WopiEditButton from "./StoredObjectButton/WopiEditButton.vue";
import {is_extension_editable, is_extension_viewable} from "./StoredObjectButton/helpers";
import {StoredObject, WopiEditButtonExecutableBeforeLeaveFunction} from "../types";

interface DocumentActionButtonsGroupConfig {
  storedObject: StoredObject,
  small?: boolean,
  canEdit?: boolean,
  canDownload?: boolean,
  canConvertPdf?: boolean,
  returnPath?: string,

  /**
   * Will be the filename displayed to the user when he·she download the document
   * (the document will be saved on his disk with this name)
   *
   * If not set, 'document' will be used.
   */
  filename?: string,

  /**
   * If set, will execute this function before leaving to the editor
   */
  executeBeforeLeave?: WopiEditButtonExecutableBeforeLeaveFunction,
}

const props = withDefaults(defineProps<DocumentActionButtonsGroupConfig>(), {
  small: false,
  canEdit: true,
  canDownload: true,
  canConvertPdf: true,
  returnPath: window.location.pathname + window.location.search + window.location.hash,
});

</script>

<style scoped>

</style>
