<script lang="ts" setup>
import { marked } from 'marked'
import { PlanFeatureTypes } from 'nocodb-sdk'

interface Prop {
  modelValue: boolean
  extensionId: string
  from: 'market' | 'extension'
}

const props = defineProps<Prop>()

const emit = defineEmits(['update:modelValue'])

const vModel = useVModel(props, 'modelValue', emit)

const {
  availableExtensions,
  descriptionContent,
  addExtension,
  getExtensionAssetsUrl,
  isMarketVisible,

  extensionAccess,
} = useExtensions()

const { blockAddNewExtension, navigateToPricing, isWsOwner } = useEeConfig()

const onBack = () => {
  vModel.value = false
  isMarketVisible.value = true
}

const onAddExtension = (ext: any) => {
  addExtension(ext)
  vModel.value = false
}

const activeExtension = computed(() => {
  return availableExtensions.value.find((ext) => ext.id === props.extensionId)
})

// Create a custom renderer
const renderer = new marked.Renderer()

// Override the image function to modify the URL
renderer.image = function (href: string, title: string | null, text: string) {
  // Modify the URL here
  const newUrl = getExtensionAssetsUrl(href)

  return `<img src="${newUrl}" alt="${text}" title="${title || ''}">`
}

// Apply the custom renderer to marked
marked.use({ renderer })

const getModifiedContent = (content = '') => {
  // Modify raw <img> tags, supporting both single and double quotes
  return content.replace(/<img\s+src=(["'])(.*?)\1(.*?)>/g, (match, quote, src, rest) => {
    const newSrc = getExtensionAssetsUrl(src)
    return `<img src=${quote}${newSrc}${quote}${rest}>`
  })
}

const detailsBody = computed(() => {
  if (descriptionContent.value[props.extensionId]) {
    return marked.parse(getModifiedContent(descriptionContent.value[props.extensionId]))
  } else if (activeExtension.value?.description) {
    return marked.parse(getModifiedContent(activeExtension.value.description))
  }

  return '<p></p>'
})
</script>

<template>
  <NcModal v-model:visible="vModel" :footer="null" size="lg" wrap-class-name="nc-modal-extension-details">
    <div v-if="activeExtension" class="flex flex-col w-full h-full">
      <div class="flex items-center gap-3 px-4 py-3 border-b-1 border-nc-border-gray-medium">
        <NcButton v-if="from === 'market'" size="small" type="text" @click="onBack">
          <GeneralIcon icon="arrowLeft" />
        </NcButton>

        <img :src="getExtensionAssetsUrl(activeExtension.iconUrl)" alt="icon" class="h-[50px] w-[50px] object-contain" />
        <div class="flex-1 flex flex-col">
          <div class="flex items-center gap-2">
            <div class="font-semibold text-xl truncate">{{ activeExtension.title }}</div>
            <NcBadgeBeta v-if="activeExtension.showAsBeta" />
          </div>
          <div class="text-small leading-[18px] text-nc-content-gray-muted truncate">{{ activeExtension.subTitle }}</div>
        </div>
        <div class="self-start flex items-center gap-2.5">
          <NcTooltip v-if="!blockAddNewExtension" :disabled="extensionAccess.create">
            <template #title>
              {{ $t('tooltip.youDoNotHaveSufficientPermissionToAddExtension') }}
            </template>
            <NcButton size="small" class="w-full" :disabled="!extensionAccess.create" @click="onAddExtension(activeExtension)">
              <div class="flex items-center justify-center gap-1 -ml-3px">
                <GeneralIcon icon="plus" /> {{ $t('general.add') }} {{ $t('general.extension') }}
              </div>
            </NcButton>
          </NcTooltip>
          <NcTooltip v-else>
            <template #title>
              {{ $t('upgrade.upgradeToAddMoreExtensions') }}
            </template>
            <NcButton
              size="small"
              class="w-full nc-upgrade-plan-btn"
              @click="navigateToPricing({ limitOrFeature: PlanFeatureTypes.FEATURE_EXTENSIONS })"
            >
              <div class="flex items-center justify-center gap-2">
                <GeneralIcon icon="ncArrowUpCircle" class="h-4 w-4" />

                {{ isWsOwner ? $t('upgrade.upgradeToAdd') : $t('upgrade.requestUpgradeToAdd') }}
              </div>
            </NcButton>
          </NcTooltip>
          <NcButton size="small" type="text" @click="vModel = false">
            <GeneralIcon icon="close" class="text-nc-content-gray-subtle2" />
          </NcButton>
        </div>
      </div>

      <div class="extension-details">
        <div class="extension-details-left">
          <div class="nc-extension-details-body" v-html="detailsBody"></div>
        </div>
        <div class="extension-details-right">
          <div class="extension-details-right-section">
            <div class="extension-details-right-title">Version</div>
            <div class="extension-details-right-subtitle">{{ activeExtension.version }}</div>
          </div>

          <NcDivider />
          <div v-if="activeExtension.publisher" class="extension-details-right-section">
            <div class="extension-details-right-title">Publisher</div>
            <div class="flex items-center gap-2">
              <img
                v-if="activeExtension.publisher?.icon?.src"
                :src="getExtensionAssetsUrl(activeExtension.publisher.icon.src)"
                alt="Publisher icon"
                class="object-contain flex-none"
                :style="{
                  width: activeExtension.publisher?.icon?.width ? `${activeExtension.publisher?.icon?.width}px` : '24px',
                  height: activeExtension.publisher?.icon?.height ? `${activeExtension.publisher?.icon?.height}px` : '24px',
                }"
              />
              <div class="extension-details-right-subtitle">{{ activeExtension.publisher.name }}</div>
            </div>
            <div class="flex items-center gap-3 text-sm font-semibold text-nc-content-brand">
              <a
                v-if="activeExtension.publisher?.url"
                :href="activeExtension.publisher.url"
                target="_blank"
                rel="noopener noreferrer"
                class="!no-underline !hover:underline"
              >
                Website
              </a>
              <template v-if="activeExtension.publisher?.email">
                <div class="border-l-1 border-nc-border-gray-medium h-5"></div>
                <a
                  :href="`mailto:${activeExtension.publisher.email}`"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="!no-underline !hover:underline"
                >
                  Contact
                </a>
              </template>
            </div>
          </div>
          <template v-if="activeExtension.links && activeExtension.links.length">
            <NcDivider />
            <div class="extension-details-right-section">
              <div class="extension-details-right-title">Links</div>
              <div>
                <div v-for="(doc, idx) of activeExtension.links" :key="idx" class="flex items-center gap-1">
                  <div class="h-7 w-7 flex items-center justify-center">
                    <GeneralIcon icon="bookOpen" class="flex-none w-4 h-4 text-nc-content-gray-subtle2" />
                  </div>
                  <a
                    :href="doc.href"
                    target="_blank"
                    rel="noopener noreferrer"
                    class="!text-nc-content-gray-subtle text-sm !no-underline !hover:underline"
                  >
                    {{ doc.title }}
                  </a>
                </div>
              </div>
            </div>
          </template>
        </div>
      </div>
    </div>
  </NcModal>
</template>

<style lang="scss" scoped>
.extension-details {
  @apply flex w-full h-[calc(100%_-_75px)];

  .extension-details-left {
    @apply p-6 flex-1 flex flex-col gap-6 nc-scrollbar-thin;
  }

  .extension-details-right {
    @apply p-5 w-[320px] flex flex-col space-y-4 border-l-1 border-nc-border-gray-medium bg-nc-bg-gray-extralight nc-scrollbar-thin;

    .extension-details-right-section {
      @apply flex flex-col gap-3;
    }

    .extension-details-right-title {
      @apply text-sm font-semibold text-nc-content-gray;
    }
    .extension-details-right-subtitle {
      @apply text-sm font-weight-500 text-nc-content-gray-subtle2;
    }
  }
}
</style>

<style lang="scss">
.nc-modal-extension-details {
  .ant-modal-content {
    @apply overflow-hidden;
  }
  .nc-modal {
    @apply !p-0;
  }

  .nc-extension-details-body {
    @apply max-w-[768px] mx-auto;

    p {
      @apply !m-0 !leading-5 text-nc-content-gray;
    }

    ul {
      li {
        @apply ml-4;
        list-style-type: disc;
      }
    }

    ol {
      @apply !pl-4;
      li {
        list-style-type: decimal;
      }
    }

    ul,
    ol {
      @apply !my-0;
    }

    // Pre tag is the parent wrapper for Code block
    pre {
      @apply overflow-auto mt-3 bg-nc-bg-gray-light;

      border-color: #d0d5dd;
      border: 1px;
      color: var(--nc-content-gray-extreme);
      font-family: 'JetBrainsMono', monospace;
      padding: 1rem;
      border-radius: 0.5rem;
      height: fit-content;

      code {
        @apply !px-0;
      }
    }

    code {
      @apply rounded-md px-2 py-1 bg-nc-bg-gray-light;

      color: inherit;
      font-size: 0.8rem;
    }

    blockquote {
      border-left: 3px solid #d0d5dd;
      padding: 0 1em;
      color: #666;
      margin: 1em 0;
      font-style: italic;
    }

    hr {
      @apply !border-nc-gray-300 !border-t-1;
      margin: 1.5em 0;
    }

    h1 {
      @apply text-nc-content-gray;
      font-weight: 700;
      font-size: 1.85rem;
      margin-bottom: 0.1rem;
      line-height: 36px;
    }

    h2 {
      @apply text-nc-content-gray;
      font-weight: 600;
      font-size: 1.55rem;
      margin-bottom: 0.1em;
      line-height: 30px;
    }

    h3 {
      @apply text-nc-content-gray;
      font-weight: 600;
      font-size: 1.15rem;
      margin-bottom: 0.1em;
      line-height: 24px;
    }
  }
}
</style>
