@charset "UTF-8";

// @description
// * px-to-cm function.
// * This function converts a value with pixel unit into centimeters.

// @access public

// @version 1.5.1

// @author Khaled Mohamed

// @license MIT

// @repository: https://github.com/Black-Axis/sass-pire

// @namespace converters

// @module converters/px-to-cm

// @dependencies:
// * - meta.type-of (SASS function).
// * - math.unit (SASS function).
// * - LibFunc.cut-unit (function).
// * - Approx.approx (function).
// * - Error.throw (function).

// @param {Number} $px-val
// * The value in pixel unit to convert.

// @throw {Exception}
// * Throws exceptions if the provided parameter is not a number, doesn't
// * have a unit, or doesn't have a pixel unit.

// @example
// * .example {
// *   content: px-to-cm(12px);
// * }

// @output
// * .example {
// *   content: 0.317cm;
// * }

// @return {Number} - Returns the converted value in centimeters.

@use "sass:meta";
@use "sass:math";
@use "../../functions/global/cut-unit" as LibFunc;
@use "../../development-utils/approximation-numbers" as Approx;
@use "../../development-utils/toggle-error-message" as Error;

@function px-to-cm($px-val) {
    @if meta.type-of($px-val) != number {
        @return Error.throw("The parameter of px-to-cm function must be in a number type.");
    }

    @if $px-val == 0 {
        @return 0;
    }

    // stylelint-disable-next-line length-zero-no-unit
    @if $px-val == 0px {
        @return Error.throw("The parameter does not need a unit if it equals to zero.");
    }

    @if math.unit($px-val) != px {
        @return Error.throw("The px-to-cm function accepts argument with pixel unit only.");
    }

    $actual-number: LibFunc.cut-unit($px-val);

    // stylelint-disable-next-line number-max-precision
    $final-result: Approx.of(($actual-number * 0.0264583333cm));

    /* stylelint-disable-next-line length-zero-no-unit */
    @if $final-result == 0px {
        @return 0;
    } @else {
        @return $final-result;
    }
}
