console.log(3)
jQuery(document).ready(function ($) {
  // お礼メッセージを表示する時間（単位はミリ秒 / 5000=5秒）
  var thanksMsgTime = 5000

  // Ajax処理
  $(".custom_like-btn").on("click", function () {
    var $target = $(this)
    var count = Number($target.find(".custom_like-count").html()) + 1
    var postID = $target.data("id")

    // ボタン要素の上辺からの距離を取得
    var btnTop = $target.offset().top
    // ポップアップ要素の高さを取得
    var popupHeight = $target.next(".custom_like-popup").outerHeight()
    // ポップアップ要素を30px上に配置
    var popupTop = btnTop - popupHeight - 30
    // ポップアップ要素の位置を設定
    $target.next(".custom_like-popup").css("top", popupTop)

    $.ajax({
      url: customLike.ajaxUrl,
      type: "POST",
      data: {
        "action": "custom_like",
        "postID": postID,
      },
      success: function (response) {
        $target.find(".custom_like-count").html(count)
        $target.addClass("custom_like--liked")
        localStorage.setItem("customLikeStyle_" + postID, "liked")

        // HTMLポップアップに「show」クラスを追加
        $target.next(".custom_like-popup").addClass("show")

        setTimeout(function () {
          $target.next(".custom_like-popup").removeClass("show")
          setTimeout(function () {
            $target.next(".custom_like-popup").remove()
          }, 300)
        }, thanksMsgTime)
      },
      error: function (e) {
        console.log(3)
      },
    })

    return false
  })

  // ページ読み込み時にスタイルを適用
  $(".custom_like-btn").each(function () {
    var $target = $(this)
    var postID = $target.data("id")
    var style = localStorage.getItem("customLikeStyle_" + postID)
    if (style === "liked") {
      $target.addClass("custom_like--liked")
      $target.prop("disabled", true)
    }
  })
})
