const APIMAPA = '';
const EXIBIR_ENDERECO = true;

var REGIAO_LOCALIZACAO_BASE = $("#REGIAO_LOCALIZACAO_BASE").val();
var STR_REGIAO_LOCALIZACAO_BASE = $("#STR_REGIAO_LOCALIZACAO_BASE").val();
var REGIAO_LOCALIZACAO_BASE_URL = $("#REGIAO_LOCALIZACAO_BASE_URL").val();

//onde o mapa vai iniciar
const lat_lng = {}
lat_lng.lat = -19.460868;
lat_lng.lng = -44.243337;

function retornarVariavelLocal() {
    //debugger;
    var local = window.location.href;

    //se for localhost
    if (local.match(/localhost/)) {
        local = local.split("/");
        local = local[0] + "//" + local[2] + '/' + local[3] + '/';
    }
    //se for producao
    else {
        local = local.split("/");
        local = local[0] + "//" + local[2] + '/';
    }

    //retorna a variavel local
    return local;
}

// retorna a url do vídeo já tratada
function retornarUrlVideoTratada(urlParam) {

    let urlVideo = "";

    //verifica se é um embed
    if (urlParam.includes("shorts/") == true) {

        let urlCompleta = urlParam; //recebe a url
        let array1 = urlCompleta.split("shorts/"); //divide em array
        let codigoVideo = array1[1]; //Esse é o codigo do vídeo

        urlVideo = "https://www.youtube.com/embed/" + codigoVideo; //Monta a url do embed
    }
    //verifica se é um embed
    else if (urlParam.includes("embed/") == true) {

        let urlCompleta = urlParam; //recebe a url
        let array1 = urlCompleta.split("embed/"); //divide em array
        let codigoVideo = array1[1]; //Esse é o codigo do vídeo

        urlVideo = "https://www.youtube.com/embed/" + codigoVideo; //Monta a url do embed        
    }
    //Verifica se tem o v= na url
    else if (urlParam.includes("v=") == true) {
        let urlCompleta = urlParam; //recebe a url
        let array1 = urlCompleta.split("v="); //corta tudo antes do v=
        let codigoVideo = array1[1]; //Esse é o codigo do vídeo

        urlVideo = "https://www.youtube.com/embed/" + codigoVideo; //Monta a url do embed
    }
    else {

        urlVideo = urlParam;
    }

    //se tiver um & comercial na url
    if (urlVideo.includes("&") == true) {

        let array2 = urlVideo.split("&");
        urlVideo = array2[0];
    }

    //console.log(urlVideo);
    return urlVideo; //Retorna a url
}

//ALERTA DE FAVORITOS
function alertFavoritos(msg) {

    $('.mensagem-alert').text(msg);

    $(".container-mansagem").animate({
        bottom: "+=100",
    }, 200, function () {
    });

    setTimeout(function () {
        $(".container-mansagem").animate({
            bottom: "-=100",
        }, 200, function () {
        });

    }, 4000)
}



if (EXIBIR_ENDERECO == false) {

    //PAGINA DE BUSCA - ESCONDER BOTÃO DO MAPA QUANDO IMOBILIÁRIA NÃO MOSTRAR O ENDEREÇO
    $('#container-btn-mapa').removeClass('d-flex');
    $('#container-btn-mapa').css('display', 'none');

    //ESCONDER BOTÃO DO MAPA NO MOBILE - PAGINA DE BUSCA
    $('#btn-mapa-mobile').css('display', 'none');

    //ENDEREÇO NO DETALHE DO IMÓVEL
    $('#breadcrumb-endereco').css('display', 'none');

    //BOTAO DO DETALHE DO IMÓVEL
    $('#btn-rua').css('display', 'none');
}


function alertSave(msg) {

    $('.mensagem-alert').text(msg);

    $(".container-mansagem").animate({
        bottom: "+=100",
    }, 200, function () {
    });

    setTimeout(function () {
        $(".container-mansagem").animate({
            bottom: "-=100",
        }, 200, function () {
        });
        document.location.reload(true);
    }, 3000)
}

function alerError(msg) {

    $('.mensagem-alert-error').text(msg);

    $(".container-mansagem-error").css('background-color', 'red');

    $(".container-mansagem-error").animate({
        bottom: "+=100",
    }, 200, function () {
    });

    setTimeout(function () {
        $(".container-mansagem-error").animate({
            bottom: "-=100",
        }, 200, function () {
        });
    }, 3000)
}



// CONVERTER PARA PLURAL


var regras = {

    /**
     * Palavras que terminam em a|e|i|o|u|ã|ãe|ão
     * apenas acrescenta a letra 's' no final
     * @type {Object}
     */
    acrescentar: {
        's': ['a', 'e', 'i', 'o', 'u', 'ã', 'ãe'],
        'es': ['r', 'z', 'n', 'ás'],
        '': ['is', 'us', 'os']
    },

    /**
     * Palavras que terminam em al|el|ol|ul|il|m
     * substitui a terminação
     * @type {Object}
     */
    substituir: {
        'ais': 'al',
        'eis': 'el',
        'ois': 'ol',
        'uis': 'ul',
        'is': 'il',
        'ns': 'm',
        'eses': 'ês',
        'ões': 'ão'
    },

    /**
     * Plural das sete exceções
     * @type {Object}
     */
    excecoes: {
        'males': 'mal',
        'cônsules': 'cônsul',
        'méis': 'mel',
        'féis': 'fel',
        'cais': 'cal',
    },

    /**
     * Palavras que não tem plural
     * @type {Object}
     */
    sem_plural: [
        'não'
    ],
};

function plural(palavra) {

    var regex_troca = "^([a-zA-Zà-úÀ-Ú]*)(%s)$"
        , plural = "";

    for (var regra in regras) {

        switch (regra) {

            case 'acrescentar':

                for (var adicao in regras[regra]) {

                    var busca = regex_troca.replace("%s", regras[regra][adicao].join("|"))
                        , regex = new RegExp(busca, 'i');

                    if (regex.exec(palavra) !== null) {
                        plural = palavra + adicao;
                        break;
                    }

                }

                break;

            case 'substituir':

                for (var substituicao in regras[regra]) {

                    var busca = regex_troca.replace("%s", regras[regra][substituicao])
                        , regex = new RegExp(busca, 'i');

                    if (regex.exec(palavra) !== null) {
                        /**
                         * Se a palavra for paroxítona ou proparoxítona,
                         * troca-se 'il' por 'eis'
                         */
                        if (palavra.match(/([áéíóú])/) !== null && regex.exec(palavra)[2] == "il") {
                            plural = palavra.replace("il", "eis");
                            break;
                        } else {
                            var busca_sub = new RegExp(regex.exec(palavra)[2] + '$', 'i');
                            plural = palavra.replace(busca_sub, substituicao);
                            break;
                        }

                    }

                }

                break;

            case 'excecoes':

                for (var excecao in regras[regra]) {
                    if (palavra == regras[regra][excecao]) {
                        plural = excecao;
                        break;
                    }
                }

                break;

            case 'sem_plural':
                regras[regra].forEach(function (r) {
                    if (palavra === r) plural = palavra;
                });

                break;
        }

    }

    return plural !== "" ? plural : palavra;

}




function getTag(imo) {

    return '<div class="container-tag">' +
        (imo.temproposta ? '<div class="tag-card">TEM PROPOSTA</div>' : '') +
        (imo.temreserva ? '<div class="tag-card">RESEVADO</div>' : '') +
        (imo.tempoCadastro < 31 ? '<div class="tag-card">NOVO</div>' : '') +
        (imo.urlpublica != '' && imo.urlpublica != undefined ? '<div class="tag-card">TOUR VIRTUAL</div>' : '') +
        (imo.exclusivo ? '<div class="tag-card">EXCLUSIVO</div>' : '') +
        '</div>';

}

function removerPlus(str) {
    // Expressão regular para encontrar os caracteres "+" no início ou no fim da string
    var regex = /^\+|\+$/g;
    // Use o método replace() para substituir os caracteres "+" por uma string vazia
    return str.replace(regex, '');
}