"use strict";

var _wp = wp,
  apiFetch = _wp.apiFetch;
jQuery(function ($) {
  var gutenify_store_baseRedirectToKitPage = function gutenify_store_baseRedirectToKitPage(res) {
    // if( res?.status && 'active' === res.status ) {
    window.location = "".concat(window.gutenify_store_base.gutenify_kit_gallery);
    // }
  };

  // Activate Gutenify.
  $(document).on('click', '.gutenify-store-base-activate-gutenify', function () {
    $(this).html('<span class="dashicons dashicons-update"></span> Loading...').addClass('gutenify-store-base-importing-gutenify');
    apiFetch({
      path: '/wp/v2/plugins/gutenify/gutenify',
      method: 'POST',
      data: {
        "status": "active"
      }
    }).then(function (res) {
      gutenify_store_baseRedirectToKitPage(res);
    })["catch"](function () {
      gutenify_store_baseRedirectToKitPage({});
    });
  });
  $(document).on('click', '.gutenify-store-base-install-gutenify', function () {
    $(this).html('<span class="dashicons dashicons-update"></span> Loading...').addClass('gutenify-store-base-importing-gutenify');
    apiFetch({
      path: '/wp/v2/plugins',
      method: 'POST',
      data: {
        "slug": "gutenify",
        "status": "active"
      }
    }).then(function (res) {
      gutenify_store_baseRedirectToKitPage(res);
    })["catch"](function () {
      gutenify_store_baseRedirectToKitPage({});
    });
  });
  $(document).on('click', '.gutenify-store-base-admin-notice .notice-dismiss', function () {
    console.log(ajaxurl + "?action=gutenify_store_base_hide_theme_info_noticebar");
    apiFetch({
      url: ajaxurl + "?action=gutenify_store_base_hide_theme_info_noticebar&gutenify_store_base-nonce-name=".concat(window.gutenify_store_base.gutenify_store_base_nonce),
      method: 'POST'
    }).then(function (res) {
      console.log(res);
    })["catch"](function (res) {
      console.log(res);
    });
  });

  // The TGM "recommended plugin" notice is rendered via settings_errors(),
  // which always adds the is-dismissible class. WP core's own "x" button
  // that class produces only fades the notice client-side - it never
  // persists anything. The actual persistence is TGM's own "Dismiss this
  // notice" link (a nonce'd request handled by TGM_Plugin_Activation::dismiss()).
  // Fire that same request in the background when the native "x" is used,
  // so both dismiss paths behave the same way.
  $(document).on('click', '#setting-error-tgmpa .notice-dismiss', function () {
    var dismissHref = $('#setting-error-tgmpa .dismiss-notice').attr('href');
    if (dismissHref) {
      $.get(dismissHref);
    }
  });

  // Open video modal 1 (Themes)
  $(document).on('click', '.btn-play-overlay-kits', function () {
    var modal = $('#gutenify-theme-modal-1');
    var iframe = modal.find('iframe');
    iframe.attr('src', iframe.attr('data-src'));
    modal.fadeIn(200);
  });

  // Open video modal 2 (Patterns)
  $(document).on('click', '.btn-play-overlay-patterns', function () {
    var modal = $('#gutenify-theme-modal-2');
    var video = modal.find('video');
    var source = video.find('source');
    source.attr('src', source.attr('data-src'));
    video[0].load();
    modal.fadeIn(200);
  });

  // Close modals
  $(document).on('click', '.gutenify-video-modal-close, .gutenify-video-modal-overlay', function (e) {
    if ($(e.target).hasClass('gutenify-video-modal-overlay') || $(e.target).hasClass('gutenify-video-modal-close')) {
      var modal = $('.gutenify-video-modal-overlay');
      modal.fadeOut(200, function () {
        // stop videos/iframes
        var iframe = modal.find('iframe');
        iframe.attr('src', '');
        var video = modal.find('video');
        if (video.length > 0) {
          video[0].pause();
          video.find('source').attr('src', '');
        }
      });
    }
  });

  // Tab switching logic
  $(document).on('click', '.gutenify-admin-tab-btn', function () {
    var tabId = $(this).data('tab');
    $('.gutenify-admin-tab-btn').removeClass('active');
    $(this).addClass('active');
    $('.gutenify-tab-content').hide();
    $('#' + tabId).fadeIn(200);
  });
});