var file_frame;
jQuery(document).ready(function(e) {
function r(r, o) {
e.trim(r.val()) || null === o || r.val(o)
}
e(".upload-image-button").on("click", function(r) {
return r.preventDefault(), file_frame ? void file_frame.open() : (file_frame = wp.media.frames.file_frame = wp.media({
title: e(this).data("uploader_title"),
button: {
text: e(this).data("uploader_button_text")
},
multiple: !1
}), file_frame.on("select", function() {
attachment = file_frame.state().get("selection").first().toJSON(), e("#lfc_book_cover_url").val(attachment.url), e("#lfc_book_cover_image").attr("src", attachment.url).show()
}), void file_frame.open())
}), e("#lfc_book_cover_url").on("change", function(r) {
"" !== e(this).val() ? e("#lfc_book_cover_image").attr("src", e(this).val()).show() : e("#lfc_book_cover_image").attr("src", "").hide()
}), e("#get-book-info").on("click", function(o) {
var t = null,
a = "",
i = "",
n = {
action: "lfc_book_get_book_info",
nonce: e("#ajax_isbn_nonce").text(),
isbn: e("#lfc_book_isbn").val().replace(/-/g, "")
};
return e.trim(e("#lfc_book_isbn").val()) ? (e(".error-details").hide(), e("#lfc-book-meta-box .spinner").addClass("is-active"), e.post(lfc_book_public_script.ajaxurl, n, function(o) {
var n, s, l = 0;
e("#lfc-book-meta-box .spinner").removeClass("is-active");
try {
if (n = JSON.parse(o), n.hasOwnProperty("data") && n.hasOwnProperty("status") && "success" === n.status)
if (s = JSON.parse(n.data), s.hasOwnProperty("items") && s.items.constructor === Array && s.items.length > 0 && s.items[0].hasOwnProperty("volumeInfo")) {
if (t = s.items[0].volumeInfo, t.hasOwnProperty("title") && r(e("#lfc_book_title"), t.title), t.hasOwnProperty("authors") && t.authors.constructor === Array && !e.trim(e("#lfc_book_author").val())) {
for (l = 0; l < t.authors.length; l++) a += t.authors[l], t.authors.length > 1 && (a += ", ");
e("#lfc_book_author").val(a)
}
if (t.hasOwnProperty("categories") && t.categories.constructor === Array && !e.trim(e("#lfc_book_genre").val())) {
for (l = 0; l < t.categories.length; l++) i += t.categories[l], t.categories.length > 1 && (i += ", ");
e("#lfc_book_genre").val(i)
}
if (t.hasOwnProperty("publisher") && r(e("#lfc_book_publisher"), t.publisher), n.releaseDate && r(e("#lfc_book_release_date"), n.releaseDate), t.hasOwnProperty("pageCount") && r(e("#lfc_book_pages"), t.pageCount), t.hasOwnProperty("imageLinks") && t.imageLinks.hasOwnProperty("thumbnail") && !e.trim(e("#lfc_book_cover_url").val()) && (e("#lfc_book_cover_url").val(t.imageLinks.thumbnail), e("#lfc_book_cover_image").attr("src", t.imageLinks.thumbnail).show()), t.hasOwnProperty("description") && "undefined" != typeof tinymce) {
var _ = tinymce.get("lfc_book_summary");
_ && _ instanceof tinymce.Editor ? e.trim(_.getContent()) || (_.setContent(t.description), _.save({
no_events: !0
})) : e.trim(e("textarea#lfc_book_summary").val()) || e("textarea#lfc_book_summary").val(t.description)
}
} else e(".error-details").text(lfc_book_google_api.not_found).show(), console.log(s);
else n.hasOwnProperty("status") && "error" === n.status && (e(".error-details").html(lfc_book_google_api.unknown_error).show(), n.hasOwnProperty("data") && console.log("Error message: " + n.data))
} catch (v) {
console.log("JSON parsing error: " + v)
}
})) : e(".error-details").text(lfc_book_google_api.no_isbn).show(), !1
})
});