// Silence may be golden, but sometimes words paint the canvas of understanding.
const { createApp } = Vue

const app = createApp({
	components: {
		LinksyTab,
		LinksyTabBar,
		LinksyButton,
		LinksyPageHeader,
		LinkReplace,
		BrokenLinks,
		RescanLinks,
		AnchorReplace,
		TitleReplace,
		DatabaseReplace,
		UrlReplace,
		LinksySearchReplaceSetup
	},
	directives: {
		'linksy-tooltip': LinksyTooltip,
	},
	data() {
		return {
			isScanningLinks: false,
			isNotificationOpened: false,
			tab: 0,
			tabs: [
				{
					id: 'link-replace',
					title: 'Link',
				},
				{
					id: 'url-replace',
					title: 'URL',
				},
				{
					id: 'broken-links',
					title: 'Broken Link',
				},
				{
					id: 'anchor-replace',
					title: 'Anchor',
				},
				{
					id: 'title-replace',
					title: 'Title',
				},
				{
					id: 'db-replace',
					title: 'Database',
				},
				{
					id: 'rescan-links',
					title: 'Rescan Links',
				},
			],
			tabsMounted: false,
			progress: 0,
			isRescanning: false,
		}
	},
	computed: {
		isSetupIncomplete: {
			get() {
				return LINKSYAI_SAR_SETUP_STATUS === 'incomplete' ? true : false
			}
		},
		activeTab: {
			get() {
				return this.tabs[this.tab]
			}
		}
	},
	methods: {
		handleTabSelect(index) {
			this.tab = index
		},

		handleTabMounted() {
			this.tabsMounted = true
		},

		async reloadPostSummary( refresh = false ) {
			this.isScanningLinks = true
			this.progress = 5

			try {
				if (refresh) this.$toast.info('Do not refresh. Rescanning posts for links... ')
				
				const {
					data: { data },
				} = await axios.get(ajaxurl, {
					params: {
						action: 'linksy_search_and_replace_sync_posts',
						nonce: LINKSY_SECURE_TOKEN,
						setup: !refresh
					},
				})

				this.progress = 100
				if (refresh) this.$toast.success('Links have been rescanned successfully.')

			} catch (error) {
				console.log(error.response?.data?.data || error.message)
			} finally {
				if (refresh) {
					this.isScanningLinks = false
				} else {
					setTimeout(() => {
						this.$toast.info('Redirecting... ')
						this.isScanningLinks = false

						window.location.replace(`${LINKSYAI_SAR.tools_url}?page=Linksy-search-and-replace`)
					}, 2000);
				}

			}
		},

		async generateLinks() {
			try {
				if (
					!confirm(
						'Use the "Rescan Broken Links" feature only when you are certain that the link statistics are incorrect. Please don\'t use it impulsively.',
					)
				) {
					return
				}
                this.isRescanning = true;

                const {
                    data: { data },
                } = await axios.get(ajaxurl, {
                    params: {
                        action: 'linksy_search_and_replace_rescan_broken_links',
                        nonce: LINKSY_SECURE_TOKEN,
                    },
                })

				this.$toast.success(data.message)
			} catch (error) {
				this.$toast.error(error.response?.data?.data || error.message)
			} finally {
                this.isRescanning = false;
            }
		}
	},
})

app.use(LinksyToast).mount('#search-and-replace-app')
