function getCookie(n) {
    var t,
        i = new RegExp("(^| )" + n + "=([^;]*)(;|$)");
    return (t = document.cookie.match(i)) ? unescape(t[2]) : null;
}

function writeCookie(n, t, i) {
    if (
        typeof _appModule != "undefined" &&
        (_appModule == "Ind" || _appModule == "Bola")
    ) {
        writeShareCookie(n, t, i);
        return;
    }
    var r = i;
    r == undefined && (r = new Date(new Date().getTime() + 1314e6));
    r = ";path=/;expires=" + r.toGMTString();
    document.cookie = n + "=" + escape(t) + r;
}
function GetSession(n) {
    return window.sessionStorage.getItem(n);
}
function convertTime(n, t) {
    var i = t == 1 ? 1 : 0,
        n;
    return (
        n.indexOf(":") != -1 &&
        ((n = n.replace(/-/g, ",").replace(/\ /g, ",").replace(/:/g, ",")),
            (i = 1)),
        (n = n.split(",")),
        (n[1] = eval(n[1]) - i),
        new Date(n[0], n[1], n[2], n[3], n[4])
    );
}
function GetHandicapType() {
    var n = 1,
        t = getCookie("Handicap_Type");
    return t && (n = parseInt(t)), n;
}

function initHandicap() {
    _handicapType = GetHandicapType();
    _handicapType <= _handicapTypeArray.length &&
        ((_handicapGive = _handicapTypeArray[_handicapType - 1].split("/")[0]),
            (_handicapAccept =
                _handicapTypeArray[_handicapType - 1].split("/")[1]));
}

function getQueryVariable(n) {
    for (
        var i, u = window.location.search.substring(1), r = u.split("&"), t = 0;
        t < r.length;
        t++
    )
        if (((i = r[t].split("=")), i[0] == n)) return i[1];
    return !1;
}

function MonitorError() {
    var n, t, i;
    getQueryVariable("mode") == "debug" &&
        ((n = getQueryVariable("report")),
            (window.onerror = function (t) {
                n == 1 && alert("运行异常" + t);
            }),
            window.addEventListener(
                "error",
                function (t) {
                    var i = t.target.src || t.target.href;
                    i && n == 1 && alert("资源加载异常:" + i);
                },
                !0
            ),
            (t = 0),
            (i = 0),
            window.addEventListener("ajaxReadyStateChange", function (r) {
                var u = r.detail,
                    e = u.status,
                    f = u.readyState;
                f === 1 && (t = new Date().getTime());
                f === 4 && (i = new Date().getTime() - t);
                f === 4 &&
                    (e === 200 || (n == 1 && alert("异常接口：" + u.responseURL)));
            }));
}

function LoadDark() {
    var n = getCookie("dark_mode") == null ? dark_mode : getCookie("dark_mode");
    GetSession("dark_mode") != null &&
        GetSession("dark_mode") != "" &&
        (n = GetSession("dark_mode"));
    document.getElementsByTagName("body").length > 0
        ? n != null && n == 1
            ? (document.getElementsByTagName("body")[0].classList.add("dark"),
                document.getElementById("flash") != null &&
                window.frames.flash.contentWindow.document.body.classList.add(
                    "dark"
                ))
            : (document
                .getElementsByTagName("body")[0]
                .classList.remove("dark"),
                document.getElementById("flash") != null &&
                window.frames.flash.contentWindow.document.body.classList.remove(
                    "dark"
                ))
        : addLoadEvent(LoadDark);
}

function BomHelper() {
    this.ie = "";
    this.firefox = "";
    this.chrome = "";
    this.opera = "";
    this.safari = "";
    this.operaMini = "";
    this.uc = "";
    this.ios = "";
    this.android = {};
    this.pc = "";
    this.kv = function (n, t, i) {
        return {
            key: n,
            value: t,
            opt: i || 1,
        };
    };
}

function mathOdds(odds) {
    // if (_oddsType != 1) return odds
    // if (odds > 1) return (-1/odds).toFixed(2);
    return odds
}

function getUTCTime() {
    const n = new Date();
    return new Date(n.getTime() + n.getTimezoneOffset() * 6e4);
}

function GetOddType() {
    var t = _defaultOddsType,
        n = getCookie("Odds_Type");
    return (
        !n == null ||
        n == "" ||
        n == undefined ||
        isNaN(n) ||
        (t = parseInt(n)),
        t
    );
}

function GetTimeZone() {
    var i = 0 - new Date().getTimezoneOffset() / 60,
        t = i,
        n = getCookie("Time_Zone");
    return (
        n == null ||
        n == "" ||
        n == undefined ||
        isNaN(n) ||
        (t = parseFloat(n)),
        writeCookie("Default_TimeZone", t),
        t
    );
}

function initSetting() {
    var r = document.getElementById("selectedOddsType"),
        u = document.getElementById("oddsType_" + _oddsType),
        n,
        t,
        i;
    if (
        (r && u && (r.innerText = u.innerText),
            (n = document.getElementById("selectedTimeZone")),
            (t = getCookie("Time_Zone")),
            n && (t == null || t == ""))
    ) {
        // Default GMT +7. Get innerText của GMT +7 để set vào selectedTimeZone
        n.innerText = document.getElementById("timeZone_7").innerText;
        return;
    }
    i = document.getElementById("timeZone_" + _timeZone);
    n && n && i && (n.innerText = i.innerText);
}

const tlt = typeof _locModel != "undefined" &&
    typeof _locModel.T.T_TIL_DataComparison != "undefined" ? _locModel.T.T_TIL_DataComparison : 'So sánh số liệu';
var _mainWebDomain = window.config.url,
    _livescoreWebDomain = "/",
    _dataWebDomain = "/data/",
    _basketballWebDomain = "/basketball/",
    _footballWebDomain = window.config.url + toThaiUri('football') + "/",
    _cmsWebDomain = "/tips/",
    _infoWebDomain = "/" + toThaiUri('football') + "/",
    _touchWebDomain = "/mobi/",
    _freeWebDomain = "/free/",
    _freeAppDomain = "/app/",
    _domain = "thscore",
    _tail = "vip",
    _defaultOddsType = "1",
    _defaultTimeZone = "8",
    _leagueNameIdx = "8",
    _isNewOddsTxt = "1",
    _upDownColorType = "1",
    _oddsOrder = "1-2-3",
    _moduleKey = "Thai",
    _websocket = !0,
    _wsUrl = "www.thscore.to:9800",
    _cdnWebDomain = "//img_cms.thscore.fun/",
    _substationType = 0,
    _isForApp = !1,
    ICONFONT = {
        DETAIL: "<i class='icon iconfont icon-icon-detail'></i>",
        LINEUP: "<i class='icon iconfont icon-font-lineup'></i>",
        ANALYSIS: "<i class='icon iconfont icon-analysis'></i>",
        ODDS: `<i title='${tlt}' class='icon iconfont icon-odds '></i>`,
        FLASH: "<i class='icon iconfont icon-matchdata'></i>",
        FLASH_V2: "<img src='/images/football-vn/btn-live-v2.svg' alt='btn-live-v2.svg' />",
        FLASHON: "<i class='icon iconfont icon-matchdata on'></i>",
        BASKETBALL: "<i class='icon iconfont icon-font-basketballclass'></i>",
        HIGHLIGHT: "<i class='icon iconfont icon-icon-live1'></i>",
        FLASH_LQ: "<i class='icon iconfont icon-b-ant-on'></i>",
        FLASHON_LQ: "<i class='icon iconfont icon-b-ant-on on'></i>",
        FAV_MATCH:
            "<i id='favM_{$MatchID}' mid='{$MatchID}' class='icon iconfont icon-font-collect-off add-info favM'></i>",
        FAV_LEAGUE:
            "<i id='favL_{$LeagueID}' lid='{$LeagueID}' class='icon iconfont icon-font-collect-off add-info-l l0 favL'></i>",
    },
    ODDSTYPE = {
        NONE: 0,
        AH: 1,
        OP: 2,
        OU: 3,
        CORNEROU: 4,
        BODAN: 5,
        EUROAH: 6,
        DOUBLECHANCE: 7,
    },
    LQHANDICAP,
    headtime,
    timeZone,
    startani_C,
    startani_A,
    startani_B,
    pop_TC,
    oPopup,
    goal,
    goalTime,
    zXml,
    X2,
    _cnzzLoaded,
    _cnzzUrls,
    dark_mode,
    bomHelper,
    ssoAvataUrl,
    _share,
    _counFavMatchTimmer,
    Storage,
    _oddsType,
    _timeZone,
    wsUtil;
Object.freeze(ODDSTYPE);
LQHANDICAP = {
    FIRST: 1,
    SECOND: 2,
    HALF: 3,
    THREE: 4,
    FOUR: 5,
    FULL: 6,
    RUNNING: 7,
};
Object.freeze(LQHANDICAP);
headtime = new Date();
timeZone = new Date().getTimezoneOffset() / -60;
try {
    oPopup = window.createPopup();
} catch (e) {
}
goal = 0;
zXml = {
    useActiveX: typeof ActiveXObject != "undefined",
    useXmlHttp: typeof XMLHttpRequest != "undefined",
};
zXml.ARR_XMLHTTP_VERS = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];

var _months2 =
    typeof _locModel != "undefined" &&
        typeof _locModel.T.T_S_Jan != "undefined"
        ? [
            _locModel.T.T_S_Jan,
            _locModel.T.T_S_Feb,
            _locModel.T.T_S_Mar,
            _locModel.T.T_S_Apr,
            _locModel.T.T_S_May,
            _locModel.T.T_S_Jun,
            _locModel.T.T_S_Jul,
            _locModel.T.T_S_Aug,
            _locModel.T.T_S_Sep,
            _locModel.T.T_S_Oct,
            _locModel.T.T_S_Nov,
            _locModel.T.T_S_Dec,
        ]
        : [
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Oct",
            "Nov",
            "Dec",
        ],
    _months = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ],
    _weeks =
        typeof _locModel != "undefined" &&
            typeof _locModel.T.T_Sunday != "undefined"
            ? [
                _locModel.T.T_Sunday,
                _locModel.T.T_Monday,
                _locModel.T.T_Tuesday,
                _locModel.T.T_Wednesday,
                _locModel.T.T_Thursday,
                _locModel.T.T_Friday,
                _locModel.T.T_Saturday,
            ]
            : [
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
            ],
    _weeks2 =
        typeof _locModel != "undefined" &&
            typeof _locModel.T.T_S_Sunday != "undefined"
            ? [
                _locModel.T.T_S_Sunday,
                _locModel.T.T_S_Monday,
                _locModel.T.T_S_Tuesday,
                _locModel.T.T_S_Wednesday,
                _locModel.T.T_S_Thursday,
                _locModel.T.T_S_Friday,
                _locModel.T.T_S_Saturday,
            ]
            : ["Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."],
    _hasInitDateFormat = 0;

var oTool = {
        USJson: {
            0.22: -450,
            0.28: -350,
            0.33: -300,
            0.36: -275,
            0.38: -267,
            0.44: -225,
            0.47: -212.5,
            0.53: -187.5,
            0.57: -175,
            0.62: -162.5,
            0.63: -160,
            0.66: -150,
            0.72: -137.5,
            0.83: -120,
            0.88: -114,
            0.91: -110,
            0.95: -105,
        },
        fractionJson: {
            0.22: "2/9",
            0.28: "2/7",
            0.33: "1/3",
            0.36: "4/11",
            0.38: "3/8",
            0.44: "4/9",
            0.47: "40/85",
            0.53: "8/15",
            0.57: "4/7",
            0.62: "8/13",
            0.63: "5/8",
            0.66: "4/6",
            0.72: "8/11",
            0.83: "5/6",
            0.88: "7/8",
            0.91: "10/11",
            0.95: "20/21",
        },
        getPL: function (n, t, i, r, u) { //u => euro (1x2)
            if (u && n != 3 && n != "3" && n != 6 && n != "6")
                return [
                    this.toFixZero(t),
                    this.toFixZero(i),
                    this.toFixZero(r),
                ];
            if (!u) {
                t = mathOdds(t)
                r = mathOdds(r)
            }
            switch (n) {
                case 1:
                case "1":
                    return [t, i, r];
                case 2:
                case "2":
                    return u ? [t, i, r] : [this.toIN(t), i, this.toIN(r)];
                case 3:
                case "3":
                    return u
                        ? [this.toUSEu(t), this.toUSEu(i), this.toUSEu(r)]
                        : [this.toUS(t), i, this.toUS(r)];
                case 4:
                case "4":
                    return [this.toEU(t), i, this.toEU(r)];
                case 5:
                case "5":
                    return u ? [t, i, r] : [this.toML(t), i, this.toML(r)];
                case 6:
                case "6":
                    return u
                        ? [
                            this.toFractionEu(t),
                            this.toFractionEu(i),
                            this.toFractionEu(r),
                        ]
                        : [this.toFraction(t), i, this.toFraction(r)];
            }
        },
          toFixZero: function (n, t) {
            t || (t = this.countDecimal(n) > 2 ? 3 : 2);
            var i = parseFloat(n);
            return i.toString() == "NaN" ? n : i.toFixed(t);
        },
        countDecimal: function (n) {
            var t = ("" + n).match(/\.(\d+)$/);
            return t === null ? 0 : t[1].length;
        },
         toML: function (n) {
            if (!n) return "";
            var t = parseFloat(n),
                i = this.countDecimal(t) > 2 ? 3 : 2;
            return t > 1 ? (0 - 1 / t).toFixed(i) : this.toFixZero(n);
        },
        toUSEu: function (n) {
            var t, u, r, i;
            return n
                ? ((t = parseFloat(n)),
                    (u = this.countDecimal(t) > 2 ? 3 : 2),
                !t || isNaN(t))
                    ? ""
                    : ((t = FloatHelper.Operator(t, 1, "-")),
                        (r = oTool.USJson),
                    t in r)
                        ? r[t]
                        : t == 0
                            ? "0"
                            : ((i = 0),
                                (i =
                                    t < 1
                                        ? Math.round(0 - 100 * (1 / t).toFixed(u))
                                        : Math.round(100 * t)),
                                i > 0 ? "+" + i : i)
                : "";
        },
    }
 _handicapTypeArray = ["/-", "+/-", "/*", "让/受让"],
    _handicapType = 1,
    _handicapAccept = "-",
    _handicapGive = "",
    _ouTrimEndZero = !0,
    GoalCn = [
        "0",
        "0/{0}0.5",
        "{0}0.5",
        "{0}0.5/1",
        "{0}1",
        "{0}1/1.5",
        "{0}1.5",
        "{0}1.5/2",
        "{0}2",
        "{0}2/2.5",
        "{0}2.5",
        "{0}2.5/3",
        "{0}3",
        "{0}3/3.5",
        "{0}3.5",
        "{0}3.5/4",
        "{0}4",
        "{0}4/4.5",
        "{0}4.5",
        "{0}4.5/5",
        "{0}5",
        "{0}5/5.5",
        "{0}5.5",
        "{0}5.5/6",
        "{0}6",
        "{0}6/6.5",
        "{0}6.5",
        "{0}6.5/7",
        "{0}7",
        "{0}7/7.5",
        "{0}7.5",
        "{0}7.5/8",
        "{0}8",
        "{0}8/8.5",
        "{0}8.5",
        "{0}8.5/9",
        "{0}9",
        "{0}9/9.5",
        "{0}9.5",
        "{0}9.5/10",
        "{0}10",
        "{0}10/10.5",
        "{0}10.5",
        "{0}10.5/11",
        "{0}11",
        "{0}11/11.5",
        "{0}11.5",
        "{0}11.5/12",
        "{0}12",
        "{0}12/12.5",
        "{0}12.5",
        "{0}12.5/13",
        "{0}13",
        "{0}13/13.5",
        "{0}13.5",
        "{0}13.5/14",
        "{0}14",
    ],
    GoalCnOU = [
        "0",
        "0/0.5",
        "0.5",
        "0.5/1",
        "1",
        "1/1.5",
        "1.5",
        "1.5/2",
        "2",
        "2/2.5",
        "2.5",
        "2.5/3",
        "3",
        "3/3.5",
        "3.5",
        "3.5/4",
        "4",
        "4/4.5",
        "4.5",
        "4.5/5",
        "5",
        "5/5.5",
        "5.5",
        "5.5/6",
        "6",
        "6/6.5",
        "6.5",
        "6.5/7",
        "7",
        "7/7.5",
        "7.5",
        "7.5/8",
        "8",
        "8/8.5",
        "8.5",
        "8.5/9",
        "9",
        "9/9.5",
        "9.5",
        "9.5/10",
        "10",
        "10/10.5",
        "10.5",
        "10.5/11",
        "11",
        "11/11.5",
        "11.5",
        "11.5/12",
        "12",
        "12/12.5",
        "12.5",
        "12.5/13",
        "13",
        "13/13.5",
        "13.5",
        "13.5/14",
        "14",
    ];
initHandicap();
_cnzzLoaded = !1;
_cnzzUrls = [];
dark_mode =
    getQueryVariable("darkmode") != !1 ? getQueryVariable("darkmode") : 0;
window.helper = () => {

    const listHtFt = () => {
        return [
            {value: '1', label: 'HT'}, {value: '0', label: 'FT'}
        ]
    }
    const listCompany = (company_id = '') => {
        const list = [ { id: 100, name: "K - Sport" },
            { value: 31, label: "Sbobet" },
            { value: 8, label: "Bet365" },
            { value: 23, label: "188bet" },
            { value: 24, label: "12bet" },
            // { value: 3, label: "Crown" },
            { value: 42, label: "18Bet" },
            { value: 1, label: "Macauslot" },
            { value: 4, label: "Ladbrokes" },
            { value: 12, label: "Easybet" },
        ];
        return company_id ? list.find(x => x.value == company_id) : list;
    };

    const getCompany = () => {
        return this.getCookie('thscore_SelCompany') || 8;
    };

    const setCompany = (company_id) => {
        setCookie('thscore_SelCompany', company_id);
    };

    const setCookie = (name, value, days = 30) => {
        const date = new Date();
        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        const expires = "expires=" + date.toUTCString();
        document.cookie = `${name}=${value}; domain=${getDoMain()}; ${expires}; path=/`;
    };

    const getCookie = (cname) => {
        const name = `${cname}=`;
        const decodedCookie = decodeURIComponent(document.cookie);
        const cookieArray = decodedCookie.split(";");

        const cookie = cookieArray.find(c => c.trim().startsWith(name));
        return cookie ? cookie.substring(name.length) : "";
    };

    return { listCompany, getCompany, setCompany, getCookie, setCookie,listHtFt };
};

function getDoMain() {
    var n = location.href.split("/")[2].split(".");
    var domain = n[0] === "www"
    ? n.slice(1).join(".")
    : n
        .map((element, index) =>
            index === n.length - 1 ? element.split(":")[0] : element
        )
        .join(".");
    return domain;
}

function getCurrentTimeZone() {
    const timeZone = getCookie("Time_Zone") || getCookie("Default_TimeZone") || 7;
    return Number.parseFloat(timeZone);
}