// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import "../../libraries/DisputeTypes.sol";

/**
 * @title LibClock
 * @notice This library contains helper functions for working with the `Clock` type.
 */
library LibClock {
    /**
     * @notice Packs a `Duration` and `Timestamp` into a `Clock` type.
     * @param _duration The `Duration` to pack into the `Clock` type.
     * @param _timestamp The `Timestamp` to pack into the `Clock` type.
     * @return clock_ The `Clock` containing the `_duration` and `_timestamp`.
     */
    function wrap(Duration _duration, Timestamp _timestamp) internal pure returns (Clock clock_) {
        assembly {
            clock_ := or(shl(0x40, _duration), _timestamp)
        }
    }

    /**
     * @notice Pull the `Duration` out of a `Clock` type.
     * @param _clock The `Clock` type to pull the `Duration` out of.
     * @return duration_ The `Duration` pulled out of `_clock`.
     */
    function duration(Clock _clock) internal pure returns (Duration duration_) {
        // Shift the high-order 64 bits into the low-order 64 bits, leaving only the `duration`.
        assembly {
            duration_ := shr(0x40, _clock)
        }
    }

    /**
     * @notice Pull the `Timestamp` out of a `Clock` type.
     * @param _clock The `Clock` type to pull the `Timestamp` out of.
     * @return timestamp_ The `Timestamp` pulled out of `_clock`.
     */
    function timestamp(Clock _clock) internal pure returns (Timestamp timestamp_) {
        // Clean the high-order 192 bits by shifting the clock left and then right again, leaving
        // only the `timestamp`.
        assembly {
            timestamp_ := shr(0xC0, shl(0xC0, _clock))
        }
    }
}
