<!--
 * index.vue
 * Copyright (C) 2024 veypi <i@veypi.com>
 * 2024-06-07 17:46
 * Distributed under terms of the MIT license.
 -->
<template>
  <div>
    <Editor style="" v-if="core.id" :eid="core.id + '.des'" v-model="preview_mode" :content="content" @save="save">
    </Editor>
  </div>
</template>

<script lang="ts" setup>
import type { models } from '#imports';
import { fs } from '@veypi/oaer'


let props = withDefaults(defineProps<{
  core: models.App,
}>(),
  {}
)

let preview_mode = ref(true)

let content = ref('')

const sync = () => {
  if (props.core.des) {
    fs.app.getFileContents(fs.app.urlunwrap(props.core.des), { format: 'text' }).then((e) => {
      content.value = e as string
    })
  }
}

watch(computed(() => props.core.id), () => {
  sync()
}, { immediate: true })

const save = (des: string) => {
  let furl = `/info/appdes/${props.core.id}.md`
  fs.app.putFileContents(furl, des).then((e) => {
    furl = fs.app.urlwrap(furl)
    if (props.core.des !== furl) {
      api.app.Patch(props.core.id, { des: furl }).then((e) => {
        preview_mode.value = true
        props.core.des = furl
      })
    } else {
      preview_mode.value = true
    }
  })
}


onMounted(() => {
})

</script>

<style scoped></style>
