/**
 * WP Content Pilot Admin
 * https://www.pluginever.com
 *
 * Copyright (c) 2019 pluginever
 * Licensed under the GPLv2+ license.
 */

/*jslint browser: true */
/*global jQuery:false */
jQuery(document).ready(function ($, window, document, undefined) {
    'use strict';

    $.wp_content_pilot_pro = {
        wait: null,
        init: function () {
            $('.wpcp-repeater').repeater();
            $('body').on('click', '#wpcp-keyword-suggester > span', this.selectSuggestedKeyword);
            $('#_keyword_suggestion').on('keyup', function () {
                var self = $.wp_content_pilot_pro;
                var item = $(this);
                if (self.wait !== null) {
                    clearTimeout(self.wait);
                }
                self.wait = setTimeout(function () {
                    self.suggestKeywords(item);
                    self.wait = null;
                }, 500);
            });
        },

        instagram: function () {
            $('#_instagram_src').on('change', function () {
                var userField = $('._instagram_user_id-field');
                if ('keyword' === $(this).val()) {
                    $.wp_content_pilot.showKeywordField();
                    userField.hide();
                } else {
                    $.wp_content_pilot.hideKeywordField();
                    userField.show();
                }
            }).change();
        },
        pinterest: function () {
            var $boardID = $('._board_id-field');
            $('#_post_from').on('change', function () {
                if ($(this).val() === 'global') {
                    $.wp_content_pilot.showKeywordField();
                    $boardID.hide();
                } else {
                    $.wp_content_pilot.hideKeywordField();
                    $boardID.show();
                }

            }).change();
        },
        vimeo: function () {
            var $userID = $('._user_id-field');
            var $filters = $('._video_filter-field');

            $('#_vimeo_search_type').on('change', function () {
                if ($(this).val() === 'global') {
                    $.wp_content_pilot.showKeywordField();
                    $userID.hide();
                    $filters.show();
                } else {
                    $.wp_content_pilot.hideKeywordField();
                    $userID.show();
                    $filters.hide();
                }

            }).change();
        },
        eventful: function () {
            $('#_category').on('change', function () {
                if ($(this).val() !== '') {
                    $('#_ex_category').val('').change();
                }
            }).change();

            $('#_ex_category').on('change', function () {
                if ($(this).val() !== '') {
                    $('#_category').val('').change();
                }
            }).change();
        },
        selectSuggestedKeyword: function (e) {
            e.preventDefault();
            var keyword = $(this).data('value');
            var keywordsInput = $('#_keywords');
            var keywords = keywordsInput.val();

            if (keywords === '') {
                keywordsInput.val(keyword);
            } else {
                keywordsInput.val(keywords + ',' + keyword);
            }

            $('#_keyword_suggestion').val('');
            $('#wpcp-keyword-suggester').html('');
            $('#wpcp-keyword-suggester').hide();
        },
        suggestKeywords: function ($el) {
            var value = $el.val();
            if (value === '') {
                return;
            }

            wp.ajax.send({
                data: {
                    action: 'wpcp_pro_get_keyword_suggestion',
                    input: value,
                    nonce: ''
                },
                success: function (res) {
                    var items = '';
                    items = $.map(res, function (word) {
                        return '<span data-value="' + word + '">' + word + '</span>';
                    });
                    items = items.join('');
                    $('#wpcp-keyword-suggester').html(items);
                    $('#wpcp-keyword-suggester').show();
                },
                error: function (error) {
                    console.log(error);
                }
            });
        },
        translator: function () {
            var deepl = ['en', 'de', 'fr', 'es', 'pt', 'pt-BR', 'it', 'nl', 'pl', 'ru', 'ja', 'zh'];
            var $translator = $('#_translator');
            var $translate_to = $('#_translate_to option');

            $translator.on('change', function () {
                $.each($translate_to, function () {
                    var option = $(this);
                    if ($.inArray(option.val(), deepl) === -1 && $translator.val() === 'deepl') {
                        option.attr('disabled', 'disabled');
                        option.attr('hidden');
                        // console.log('dis');
                    } else {
                        option.removeAttr('disabled');
                        option.removeAttr('hidden');
                    }
                });

            }).change();
        }
    };


    $.wp_content_pilot_pro.init();
    $.wp_content_pilot_pro.instagram();
    $.wp_content_pilot_pro.translator();
    $.wp_content_pilot_pro.pinterest();
    $.wp_content_pilot_pro.vimeo();
    $.wp_content_pilot_pro.eventful();
});

