---
import '../styles/global.css';
import '../styles/refresh.css';
import '../styles/site-footer.css';
interface Props {
  title?: string;
  description?: string;
  ogType?: 'website' | 'article';
  image?: string;
  imageAlt?: string;
  publishedTime?: string;
  updatedTime?: string;
  structuredData?: Record<string, unknown> | Record<string, unknown>[];
  robots?: string;
}
const {
  title = 'MoreminiMore | เพิ่มกำไรให้ลูกค้า',
  description = 'พาร์ทเนอร์สำหรับ SME ที่อยากตัดสินใจให้คุ้มขึ้น ด้วยเว็บไซต์ การตลาด AI และระบบอัตโนมัติที่ยึดข้อมูลจริง',
  ogType = 'website',
  image = '/og.png',
  imageAlt = 'MoreminiMore — เป้าหมายของเราคือเพิ่มกำไรให้ลูกค้า',
  publishedTime,
  updatedTime,
  structuredData,
  robots = 'index, follow, max-image-preview:large',
} = Astro.props;
const canonicalURL = new URL(Astro.url.pathname, 'https://moreminimore.com');
const socialImageURL = new URL(image, 'https://moreminimore.com');
const organization = { '@context': 'https://schema.org', '@type': 'Organization', '@id': 'https://moreminimore.com/#organization', name: 'MoreminiMore', url: 'https://moreminimore.com/', description: 'ที่ปรึกษาด้านเว็บไซต์ การตลาดออนไลน์ AI และระบบอัตโนมัติสำหรับ SME', telephone: '+66809955945', email: 'contact@moreminimore.com', sameAs: ['https://line.me/R/ti/p/@moreminimore'] };
const website = { '@context': 'https://schema.org', '@type': 'WebSite', '@id': 'https://moreminimore.com/#website', url: 'https://moreminimore.com/', name: 'MoreminiMore', publisher: { '@id': 'https://moreminimore.com/#organization' }, inLanguage: 'th-TH' };
const isHome = Astro.url.pathname === '/';
const enableTracking = import.meta.env.PROD;
---
<!doctype html>
<html lang="th">
  <head>
    <meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="theme-color" content="#feb400" /><meta name="description" content={description} /><meta name="robots" content={robots} /><link rel="canonical" href={canonicalURL} />
    <link rel="preconnect" href="https://fonts.googleapis.com" /><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Thai:wght@400;500;600;700&family=Manrope:wght@500;600;700;800&display=swap" rel="stylesheet" />
    <meta property="og:type" content={ogType} /><meta property="og:locale" content="th_TH" /><meta property="og:site_name" content="MoreminiMore" /><meta property="og:title" content={title} /><meta property="og:description" content={description} /><meta property="og:url" content={canonicalURL} /><meta property="og:image" content={socialImageURL} /><meta property="og:image:alt" content={imageAlt} />
    {publishedTime && <meta property="article:published_time" content={publishedTime} />}{updatedTime && <meta property="article:modified_time" content={updatedTime} />}
    <meta name="twitter:card" content="summary_large_image" /><meta name="twitter:title" content={title} /><meta name="twitter:description" content={description} /><meta name="twitter:image" content={socialImageURL} /><meta name="twitter:image:alt" content={imageAlt} />
    <title>{title}</title><script is:inline type="application/ld+json" set:html={JSON.stringify(organization)}></script>{isHome && <script is:inline type="application/ld+json" set:html={JSON.stringify(website)}></script>}{structuredData && <script is:inline type="application/ld+json" set:html={JSON.stringify(structuredData)}></script>}
    {enableTracking && <Fragment>
      <!-- Safe bootstrap only: no analytics or advertising scripts are requested here. -->
      <script is:inline>
        window.dataLayer = window.dataLayer || [];
        window.gtag = function(){ window.dataLayer.push(arguments); };
        window.gtag('consent', 'default', {
          ad_storage: 'denied',
          ad_user_data: 'denied',
          ad_personalization: 'denied',
          analytics_storage: 'denied',
          functionality_storage: 'denied',
          personalization_storage: 'denied',
          security_storage: 'granted',
          wait_for_update: 1000
        });

        window.__moreminiTrackingConsent = { analytics: false, marketing: false };

        function loadConsentScript(id, src, attributes) {
          if (document.getElementById(id)) return;
          var script = document.createElement('script');
          script.id = id;
          script.src = src;
          script.async = true;
          Object.keys(attributes || {}).forEach(function(name) {
            script.setAttribute(name, attributes[name]);
          });
          document.head.appendChild(script);
        }

        function enableAnalytics() {
          var state = window.__moreminiTrackingConsent;
          if (state.analytics) return;
          state.analytics = true;
          window.gtag('consent', 'update', { analytics_storage: 'granted' });
          loadConsentScript('moremini-ga4', 'https://www.googletagmanager.com/gtag/js?id=G-74BHREDLC3', {});
          window.gtag('js', new Date());
          window.gtag('config', 'G-74BHREDLC3', { allow_enhanced_conversions: true });
          loadConsentScript('moremini-umami', 'https://umami.moreminimore.com/script.js', {
            'data-website-id': 'b2e87a6c-0b64-43c8-bb09-e406ffca0af1'
          });
          loadConsentScript('moremini-recorder', 'https://umami.moreminimore.com/recorder.js', {
            'data-website-id': 'b2e87a6c-0b64-43c8-bb09-e406ffca0af1',
            'data-sample-rate': '0.15',
            'data-mask-level': 'strict',
            'data-block-selector': '.contact-form',
            'data-max-duration': '300000'
          });
        }

        function enableMarketing() {
          var state = window.__moreminiTrackingConsent;
          if (state.marketing) return;
          state.marketing = true;
          window.gtag('consent', 'update', {
            ad_storage: 'granted',
            ad_user_data: 'granted',
            ad_personalization: 'granted'
          });
          !function(f,b,e,v,n,t,s)
          {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
          n.callMethod.apply(n,arguments):n.queue.push(arguments)};
          if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
          n.queue=[];t=b.createElement(e);t.async=!0;t.id='moremini-meta';
          t.src=v;s=b.getElementsByTagName(e)[0];
          s.parentNode.insertBefore(t,s)}(window, document,'script',
          'https://connect.facebook.net/en_US/fbevents.js');
          window.fbq('consent', 'grant');
          window.fbq('init', '418349260078648');
          var pvEventId = 'pv_' + Date.now() + '_' + Math.random().toString(36).slice(2,11);
          window.fbq('track', 'PageView', {}, {eventID: pvEventId});
        }

        function applyConsent(accepted) {
          var categories = Array.isArray(accepted) ? accepted : [];
          var state = window.__moreminiTrackingConsent;
          var analyticsAllowed = categories.includes('analytics');
          var marketingAllowed = categories.includes('marketing');
          if ((state.analytics && !analyticsAllowed) || (state.marketing && !marketingAllowed)) {
            if (window.fbq && !marketingAllowed) window.fbq('consent', 'revoke');
            window.location.reload();
            return;
          }
          if (analyticsAllowed) enableAnalytics();
          if (marketingAllowed) enableMarketing();
        }

        document.addEventListener('consentos:consent-change', function(event) {
          applyConsent(event && event.detail ? event.detail.accepted : []);
        });

        window.generateEventId = function(prefix) {
          return (prefix || 'evt') + '_' + Date.now() + '_' + Math.random().toString(36).slice(2,11);
        };
        window.trackMarketingEvent = function(eventName, eventData, metaEventName, options) {
          var data = eventData || {};
          var settings = options || {};
          var eventId = settings.eventId || window.generateEventId(eventName);
          var consent = window.__moreminiTrackingConsent;
          if (!settings.skipGoogle && consent.analytics && window.gtag) window.gtag('event', eventName, data);
          var metaName = metaEventName || eventName;
          var metaMethod = ['Contact', 'ViewContent', 'Lead'].includes(metaName) ? 'track' : 'trackCustom';
          if (!settings.skipMeta && consent.marketing && window.fbq) window.fbq(metaMethod, metaName, data, {eventID: eventId});
          if (!settings.skipUmami && consent.analytics && window.umami && window.umami.track) window.umami.track(eventName, data);
        };
        document.addEventListener('click', function(event) {
          if (!(event.target instanceof Element)) return;
          var link = event.target.closest('a[href]');
          if (!link) return;
          var href = link.getAttribute('href') || '';
          var locationName = link.closest('header') ? 'header' : (link.closest('footer') ? 'footer' : (link.closest('section')?.id || 'page'));
          if (href.startsWith('tel:')) {
            window.trackMarketingEvent('contact_clicked', {method: 'phone', location: locationName}, 'Contact');
          } else if (href.startsWith('mailto:')) {
            window.trackMarketingEvent('contact_clicked', {method: 'email', location: locationName}, 'Contact');
          } else if (href.includes('line.me')) {
            window.trackMarketingEvent('contact_clicked', {method: 'line', location: locationName}, 'Contact');
          } else if (href.startsWith('/services/')) {
            window.trackMarketingEvent('service_selected', {service_path: href, location: locationName}, 'ViewContent');
          }
        }, {capture: true});
      </script>

      <!-- ConsentOS emits consentos:consent-change with accepted category slugs. -->
      <script is:inline src="https://consent.moreminimore.com/consent-loader.js" data-site-id="2f6d0ab5-f7d6-4d06-b299-5069c21f6238" data-api-base="https://consent.moreminimore.com"></script>
    </Fragment>}
  </head>
  <body>
    <a class="skip-link" href="#content">ข้ามไปยังเนื้อหา</a><slot />
  </body>
</html>
