"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var utils_1 = require("@joincivil/utils");
var PhaseCountdown_1 = require("../PhaseCountdown");
var styledComponents_1 = require("./styledComponents");
var textComponents_1 = require("./textComponents");
var PhaseCountdown = function (props) {
    var expiry;
    var isInApplication = props.isInApplication, inChallengeCommitVotePhase = props.inChallengeCommitVotePhase, inChallengeRevealPhase = props.inChallengeRevealPhase, isAwaitingAppealRequest = props.isAwaitingAppealRequest, isAwaitingAppealJudgement = props.isAwaitingAppealJudgement, isAwaitingAppealChallenge = props.isAwaitingAppealChallenge, isInAppealChallengeCommitPhase = props.isInAppealChallengeCommitPhase, isInAppealChallengeRevealPhase = props.isInAppealChallengeRevealPhase, appExpiry = props.appExpiry, commitEndDate = props.commitEndDate, revealEndDate = props.revealEndDate, requestAppealExpiry = props.requestAppealExpiry, appealPhaseExpiry = props.appealPhaseExpiry, appealOpenToChallengeExpiry = props.appealOpenToChallengeExpiry, appealChallengeCommitEndDate = props.appealChallengeCommitEndDate, appealChallengeRevealEndDate = props.appealChallengeRevealEndDate;
    if (isInApplication) {
        expiry = appExpiry;
    }
    else if (inChallengeCommitVotePhase) {
        expiry = commitEndDate;
    }
    else if (inChallengeRevealPhase) {
        expiry = revealEndDate;
    }
    else if (isAwaitingAppealRequest) {
        expiry = requestAppealExpiry;
    }
    else if (isAwaitingAppealJudgement) {
        expiry = appealPhaseExpiry;
    }
    else if (isAwaitingAppealChallenge) {
        expiry = appealOpenToChallengeExpiry;
    }
    else if (isInAppealChallengeCommitPhase) {
        expiry = appealChallengeCommitEndDate;
    }
    else if (isInAppealChallengeRevealPhase) {
        expiry = appealChallengeRevealEndDate;
    }
    var warn = inChallengeCommitVotePhase ||
        inChallengeRevealPhase ||
        isInAppealChallengeCommitPhase ||
        isInAppealChallengeRevealPhase;
    if (!expiry) {
        return null;
    }
    expiry = parseInt(expiry.toString(), 10);
    return React.createElement(PhaseCountdown_1.TextCountdownTimer, { endTime: expiry, warn: warn });
};
/**
 * Renders a human-readable timestamp for phases that have no expiry
 */
var Timestamp = function (props) {
    var timestamp = 0;
    var LabelTextComponent = function () { return React.createElement(React.Fragment, null); };
    var canBeWhitelisted = props.canBeWhitelisted, appExpiry = props.appExpiry, canResolveChallenge = props.canResolveChallenge, revealEndDate = props.revealEndDate, canListingAppealChallengeBeResolved = props.canListingAppealChallengeBeResolved, appealChallengeRevealEndDate = props.appealChallengeRevealEndDate, isWhitelisted = props.isWhitelisted, isUnderChallenge = props.isUnderChallenge, whitelistedTimestamp = props.whitelistedTimestamp;
    // Unchallenged application
    if (canBeWhitelisted && appExpiry) {
        timestamp = appExpiry;
        LabelTextComponent = textComponents_1.ApplicationPhaseEndedLabelText;
        // Resolve Challenge
    }
    else if (canResolveChallenge && revealEndDate) {
        timestamp = revealEndDate;
        LabelTextComponent = textComponents_1.ChallengeEndedLabelText;
        // Resolve Appeal Challenge
    }
    else if (canListingAppealChallengeBeResolved && appealChallengeRevealEndDate) {
        timestamp = appealChallengeRevealEndDate;
        LabelTextComponent = textComponents_1.ChallengeEndedLabelText;
        // Whitelisted and not Under Challenge
    }
    else if (isWhitelisted && !isUnderChallenge && whitelistedTimestamp) {
        timestamp = whitelistedTimestamp;
        LabelTextComponent = textComponents_1.ApprovedLabelText;
    }
    if (!timestamp) {
        return null;
    }
    var timestampStrings = utils_1.getLocalDateTimeStrings(timestamp);
    return (React.createElement(styledComponents_1.MetaRow, null,
        React.createElement(styledComponents_1.MetaItemLabel, null,
            React.createElement(LabelTextComponent, null)),
        React.createElement(styledComponents_1.MetaItemValue, null,
            timestampStrings[0],
            " ",
            timestampStrings[1])));
};
var PhaseCountdownOrTimestamp = function (props) {
    var isInApplication = props.isInApplication, inChallengeCommitVotePhase = props.inChallengeCommitVotePhase, inChallengeRevealPhase = props.inChallengeRevealPhase, isAwaitingAppealRequest = props.isAwaitingAppealRequest, isAwaitingAppealJudgement = props.isAwaitingAppealJudgement, isAwaitingAppealChallenge = props.isAwaitingAppealChallenge, isInAppealChallengeCommitPhase = props.isInAppealChallengeCommitPhase, isInAppealChallengeRevealPhase = props.isInAppealChallengeRevealPhase;
    if (isInApplication ||
        inChallengeCommitVotePhase ||
        inChallengeRevealPhase ||
        isAwaitingAppealRequest ||
        isAwaitingAppealJudgement ||
        isAwaitingAppealChallenge ||
        isInAppealChallengeCommitPhase ||
        isInAppealChallengeRevealPhase) {
        return React.createElement(PhaseCountdown, __assign({}, props));
    }
    else {
        return React.createElement(Timestamp, __assign({}, props));
    }
};
exports.default = PhaseCountdownOrTimestamp;
//# sourceMappingURL=PhaseCountdownOrTimestamp.js.map