import { BlockPreview } from '@wordpress/block-editor'
import { rawHandler } from '@wordpress/blocks'
import { useRef, useMemo, useEffect, useState } from '@wordpress/element'
import { __, sprintf } from '@wordpress/i18n'
import classNames from 'classnames'
import { Templates as TemplatesApi } from '@library/api/Templates'
import { useIsDevMode } from '@library/hooks/helpers'
import { AuthorizationCheck, Middleware } from '@library/middleware'
import { useGlobalStore } from '@library/state/GlobalState'
import { useUserStore } from '@library/state/User'
import { injectTemplateBlocks } from '@library/util/templateInjection'
import { DevButtonOverlay } from './DevHelpers'
import { NoImportModal } from './modals/NoImportModal'
import { ProModal } from './modals/ProModal'
const canImportMiddleware = Middleware([
'hasRequiredPlugins',
'hasPluginsActivated',
])
export function ImportTemplateBlock({ template, maxHeight }) {
const importButtonRef = useRef(null)
const hasAvailableImports = useUserStore(
(state) => state.hasAvailableImports,
)
const loggedIn = useUserStore((state) => state.apiKey.length)
const setOpen = useGlobalStore((state) => state.setOpen)
const pushModal = useGlobalStore((state) => state.pushModal)
const removeAllModals = useGlobalStore((state) => state.removeAllModals)
const [topValue, setTopValue] = useState(0)
const type = Array.isArray(template?.fields?.type)
? template.fields.type[0]
: template?.fields?.type
const blocks = useMemo(
() => rawHandler({ HTML: halfImageSizes(template.fields.code) }),
[template.fields.code],
)
// The above will cut the image sizes in half, and the below will be inserted into the page
const blocksRaw = useMemo(
() => rawHandler({ HTML: template.fields.code }),
[template.fields.code],
)
const devMode = useIsDevMode()
const importTemplates = async () => {
await canImportMiddleware.check(template)
AuthorizationCheck(canImportMiddleware)
.then(() => {
setTimeout(() => {
injectTemplateBlocks(blocksRaw, template)
.then(() => removeAllModals())
.then(() => setOpen(false))
.then(() => canImportMiddleware.reset())
}, 100)
})
.catch(() => {})
}
const handleKeyDown = (event) => {
if (['Enter', 'Space', ' '].includes(event.key)) {
event.stopPropagation()
event.preventDefault()
importTemplate()
}
}
const importTemplate = () => {
// Make a note that they attempted to import
TemplatesApi.maybeImport(template)
if (template?.fields?.pro && !loggedIn) {
pushModal(