<commit_before>const moment               = require('moment');
const ViewPopupUI          = require('./view_popup.ui');
const BinarySocket         = require('../../socket');
const Highchart            = require('../../trade/charts/highchart');
const TickDisplay          = require('../../trade/tick_trade');
const showLocalTimeOnHover = require('../../../base/clock').showLocalTimeOnHover;
const toJapanTimeIfNeeded  = require('../../../base/clock').toJapanTimeIfNeeded;
const setViewPopupTimer    = require('../../../base/clock').setViewPopupTimer;
const localize             = require('../../../base/localize').localize;
const State                = require('../../../base/storage').State;
const urlFor               = require('../../../base/url').urlFor;
const createElement        = require('../../../base/utility').createElement;
const getPropertyValue     = require('../../../base/utility').getPropertyValue;
const isEmptyObject        = require('../../../base/utility').isEmptyObject;
const jpClient             = require('../../../common_functions/country_base').jpClient;
const addComma             = require('../../../common_functions/currency').addComma;
const formatMoney          = require('../../../common_functions/currency').formatMoney;

const ViewPopup = (() => {
    let contract_id,
        contract,
        is_sold,
        is_sell_clicked,
        chart_started,
        chart_init,
        chart_updated,
        sell_text_updated,
        btn_view,
        $container,
        $loading;

    const popupbox_id  = 'inpage_popup_content_box';
    const wrapper_id   = 'sell_content_wrapper';
    const hidden_class = 'invisible';

    const init = (button) => {
        btn_view          = button;
        contract_id       = $(btn_view).attr('contract_id');
        contract          = {};
        is_sold           = false;
        is_sell_clicked   = false;
        chart_started     = false;
        chart_init        = false;
        chart_updated     = false;
        sell_text_updated = false;
        $container        = '';

        if (btn_view) {
            ViewPopupUI.disableButton($(btn_view));
            ViewPopupUI.cleanup();
        }

        getContract();

        setLoadingState(true);
    };

    const responseContract = (response) => {
        if (!response.proposal_open_contract || isEmptyObject(response.proposal_open_contract)) {
            showErrorPopup(response);
            return;
        }
        // In case of error such as legacy shortcode, this call is returning the error message
        // but no error field. To specify those cases, we check for other fields existence
        if (!getPropertyValue(response, ['proposal_open_contract', 'shortcode'])) {
            showErrorPopup(response, response.proposal_open_contract.validation_error);
            return;
        }

        $.extend(contract, response.proposal_open_contract);

        if (contract && document.getElementById(wrapper_id)) {
            update();
            return;
        }

        showContract();
    };

    const showContract = () => {
        setLoadingState(false);

        if (!$container) {
            $container = makeTemplate();
        }

        containerSetText('trade_details_contract_id', contract.contract_id);

        containerSetText('trade_details_start_date', toJapanTimeIfNeeded(epochToDateTime(contract.date_start)));
        if (document.getElementById('trade_details_end_date')) containerSetText('trade_details_end_date', toJapanTimeIfNeeded(epochToDateTime(contract.date_expiry)));
        containerSetText('trade_details_payout', formatMoney(contract.currency, contract.payout));
        containerSetText('trade_details_purchase_price', formatMoney(contract.currency, contract.buy_price));

        setViewPopupTimer(updateTimers);
        update();
        ViewPopupUI.repositionConfirmation();

        if (State.get('is_mb_trading')) {
            State.call('ViewPopup.onDisplayed');
        }
    };

    const update = () => {
        const final_price      = contract.sell_price || contract.bid_price;
        const is_started       = !contract.is_forward_starting || contract.current_spot_time > contract.date_start;
        const user_sold        = contract.sell_time && contract.sell_time < contract.date_expiry;
        const is_ended         = contract.is_settleable || contract.is_sold || user_sold;
        const indicative_price = final_price && is_ended ? final_price : (contract.bid_price || null);

        if (contract.barrier_count > 1) {
            containerSetText('trade_details_barrier', addComma(contract.high_barrier), '', true);
            containerSetText('trade_details_barrier_low', addComma(contract.low_barrier), '', true);
        } else if (contract.barrier) {
            const formatted_barrier = addComma(contract.barrier);
            const mapping           = {
                DIGITMATCH: 'Equals',
                DIGITDIFF : 'Not',
            };
            const contract_text     = mapping[contract.contract_type];
            const barrier_prefix    = contract_text ? `${localize(contract_text)} ` : '';
            containerSetText(
                'trade_details_barrier',
                contract.entry_tick_time ? (barrier_prefix + formatted_barrier) : '-',
                '',
                true);
        }

        let current_spot      = contract.current_spot;
        let current_spot_time = contract.current_spot_time;
        if (is_ended) {
            current_spot      = user_sold ? '' : contract.exit_tick;
            current_spot_time = user_sold ? '' : contract.exit_tick_time;
        }

        if (current_spot) {
            containerSetText('trade_details_current_spot > span', addComma(current_spot));
        } else {
            $('#trade_details_current_spot').parent().setVisibility(0);
        }

        if (current_spot_time) {
            if (window.time && current_spot_time > window.time.unix()) {
                window.time = moment(current_spot_time).utc();
                updateTimers();
            }
            containerSetText('trade_details_current_date', toJapanTimeIfNeeded(epochToDateTime(current_spot_time)));
        } else {
            $('#trade_details_current_date').parent().setVisibility(0);
        }

        containerSetText('trade_details_ref_id', contract.transaction_ids.buy + (contract.transaction_ids.sell ? ` - ${contract.transaction_ids.sell}` : ''));
        containerSetText('trade_details_indicative_price', indicative_price ? formatMoney(contract.currency, indicative_price) : '-');

        let profit_loss,
            percentage;

        if (final_price) {
            profit_loss = final_price - contract.buy_price;
            percentage  = addComma((profit_loss * 100) / contract.buy_price, 2);
            containerSetText('trade_details_profit_loss',
                `${formatMoney(contract.currency, profit_loss)}<span class="percent">(${(percentage > 0 ? '+' : '')}${percentage}%)</span>`, { class: (profit_loss >= 0 ? 'profit' : 'loss') });
        } else {
            containerSetText('trade_details_profit_loss', '-', { class: 'loss' });
        }

        if (!is_started) {
            containerSetText('trade_details_entry_spot > span', '-');
            containerSetText('trade_details_message', localize('Contract has not started yet'));
        } else {
            if (contract.entry_spot > 0) {
                containerSetText('trade_details_entry_spot > span', addComma(contract.entry_spot));
            }
            containerSetText('trade_details_message', contract.validation_error ? contract.validation_error : '&nbsp;');
        }

        if (!chart_started && !contract.tick_count) {
            if (!chart_init) {<commit_msg>don't show entry spot or barrier if contract sold before start time<commit_after>const moment               = require('moment');
const ViewPopupUI          = require('./view_popup.ui');
const BinarySocket         = require('../../socket');
const Highchart            = require('../../trade/charts/highchart');
const TickDisplay          = require('../../trade/tick_trade');
const showLocalTimeOnHover = require('../../../base/clock').showLocalTimeOnHover;
const toJapanTimeIfNeeded  = require('../../../base/clock').toJapanTimeIfNeeded;
const setViewPopupTimer    = require('../../../base/clock').setViewPopupTimer;
const localize             = require('../../../base/localize').localize;
const State                = require('../../../base/storage').State;
const urlFor               = require('../../../base/url').urlFor;
const createElement        = require('../../../base/utility').createElement;
const getPropertyValue     = require('../../../base/utility').getPropertyValue;
const isEmptyObject        = require('../../../base/utility').isEmptyObject;
const jpClient             = require('../../../common_functions/country_base').jpClient;
const addComma             = require('../../../common_functions/currency').addComma;
const formatMoney          = require('../../../common_functions/currency').formatMoney;

const ViewPopup = (() => {
    let contract_id,
        contract,
        is_sold,
        is_sell_clicked,
        chart_started,
        chart_init,
        chart_updated,
        sell_text_updated,
        btn_view,
        $container,
        $loading;

    const popupbox_id  = 'inpage_popup_content_box';
    const wrapper_id   = 'sell_content_wrapper';
    const hidden_class = 'invisible';

    const init = (button) => {
        btn_view          = button;
        contract_id       = $(btn_view).attr('contract_id');
        contract          = {};
        is_sold           = false;
        is_sell_clicked   = false;
        chart_started     = false;
        chart_init        = false;
        chart_updated     = false;
        sell_text_updated = false;
        $container        = '';

        if (btn_view) {
            ViewPopupUI.disableButton($(btn_view));
            ViewPopupUI.cleanup();
        }

        getContract();

        setLoadingState(true);
    };

    const responseContract = (response) => {
        if (!response.proposal_open_contract || isEmptyObject(response.proposal_open_contract)) {
            showErrorPopup(response);
            return;
        }
        // In case of error such as legacy shortcode, this call is returning the error message
        // but no error field. To specify those cases, we check for other fields existence
        if (!getPropertyValue(response, ['proposal_open_contract', 'shortcode'])) {
            showErrorPopup(response, response.proposal_open_contract.validation_error);
            return;
        }

        $.extend(contract, response.proposal_open_contract);

        if (contract && document.getElementById(wrapper_id)) {
            update();
            return;
        }

        showContract();
    };

    const showContract = () => {
        setLoadingState(false);

        if (!$container) {
            $container = makeTemplate();
        }

        containerSetText('trade_details_contract_id', contract.contract_id);

        containerSetText('trade_details_start_date', toJapanTimeIfNeeded(epochToDateTime(contract.date_start)));
        if (document.getElementById('trade_details_end_date')) containerSetText('trade_details_end_date', toJapanTimeIfNeeded(epochToDateTime(contract.date_expiry)));
        containerSetText('trade_details_payout', formatMoney(contract.currency, contract.payout));
        containerSetText('trade_details_purchase_price', formatMoney(contract.currency, contract.buy_price));

        setViewPopupTimer(updateTimers);
        update();
        ViewPopupUI.repositionConfirmation();

        if (State.get('is_mb_trading')) {
            State.call('ViewPopup.onDisplayed');
        }
    };

    const update = () => {
        const final_price       = contract.sell_price || contract.bid_price;
        const is_started        = !contract.is_forward_starting || contract.current_spot_time > contract.date_start;
        const user_sold         = contract.sell_time && contract.sell_time < contract.date_expiry;
        const is_ended          = contract.is_settleable || contract.is_sold || user_sold;
        const indicative_price  = final_price && is_ended ? final_price : (contract.bid_price || null);
        const sold_before_start = contract.sell_time && contract.sell_time < contract.date_start;

        if (contract.barrier_count > 1) {
            containerSetText('trade_details_barrier', sold_before_start ? '-' : addComma(contract.high_barrier), '', true);
            containerSetText('trade_details_barrier_low', sold_before_start ? '-' : addComma(contract.low_barrier), '', true);
        } else if (contract.barrier) {
            const formatted_barrier = addComma(contract.barrier);
            const mapping           = {
                DIGITMATCH: 'Equals',
                DIGITDIFF : 'Not',
            };
            const contract_text     = mapping[contract.contract_type];
            const barrier_prefix    = contract_text ? `${localize(contract_text)} ` : '';
            // only show entry spot if available and contract was not sold before start time
            containerSetText(
                'trade_details_barrier',
                contract.entry_tick_time && sold_before_start ? '-' : (barrier_prefix + formatted_barrier),
                '',
                true);
        }

        let current_spot      = contract.current_spot;
        let current_spot_time = contract.current_spot_time;
        if (is_ended) {
            current_spot      = user_sold ? '' : contract.exit_tick;
            current_spot_time = user_sold ? '' : contract.exit_tick_time;
        }

        if (current_spot) {
            containerSetText('trade_details_current_spot > span', addComma(current_spot));
        } else {
            $('#trade_details_current_spot').parent().setVisibility(0);
        }

        if (current_spot_time) {
            if (window.time && current_spot_time > window.time.unix()) {
                window.time = moment(current_spot_time).utc();
                updateTimers();
            }
            containerSetText('trade_details_current_date', toJapanTimeIfNeeded(epochToDateTime(current_spot_time)));
        } else {
            $('#trade_details_current_date').parent().setVisibility(0);
        }

        containerSetText('trade_details_ref_id', contract.transaction_ids.buy + (contract.transaction_ids.sell ? ` - ${contract.transaction_ids.sell}` : ''));
        containerSetText('trade_details_indicative_price', indicative_price ? formatMoney(contract.currency, indicative_price) : '-');

        let profit_loss,
            percentage;

        if (final_price) {
            profit_loss = final_price - contract.buy_price;
            percentage  = addComma((profit_loss * 100) / contract.buy_price, 2);
            containerSetText('trade_details_profit_loss',
                `${formatMoney(contract.currency, profit_loss)}<span class="percent">(${(percentage > 0 ? '+' : '')}${percentage}%)</span>`, { class: (profit_loss >= 0 ? 'profit' : 'loss') });
        } else {
            containerSetText('trade_details_profit_loss', '-', { class: 'loss' });
        }

        if (!is_started) {
            containerSetText('trade_details_entry_spot > span', '-');
            containerSetText('trade_details_message', localize('Contract has not started yet'));
        } else {
            if (contract.entry_spot > 0) {
                // only show entry spot if available and contract was not sold before start time
                containerSetText('trade_details_entry_spot > span', sold_before_start ? '-' : addComma(contract.entry_spot));
            }
            containerSetText('trade_details_message', contract.validation_error ? contract.validation_error : '&nbsp;');
        }

        if (!chart_started && !contract.tick_count) {
            if (!chart_init) {
{'fraction_non_alphanumeric': 0.12487462387161484, 'fraction_numerical': 0.0012537612838515546, 'mean_word_length': 2.3978700745473907, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1918819', 'n_tokens_mistral': 4430, 'n_tokens_neox': 4239, 'n_words': 724}