//
//  Copyright (c) 2021 GrowStocks
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.
//

@use '../Tools';
@use '../Feature';

@use 'sass:map';
@use 'sass:list';

$_valid-animations: (none, loading, scale, rotate, translate);
$_valid-settings: (none, scaling, translate, rotation);

@mixin apply(
    $animation-type: none,
    $animation-settings: none
) {
    $_animation-type: verify-type_($animation-type);
    $_animation-settings: $animation-settings;

    @if Feature.has-item($_animation-type, loading) {

        animation: rotate 1.7s linear infinite;

        @keyframes rotate {
            from {
                @include Tools.prefix(transform, rotate(0deg));
            }
            to {
                @include Tools.prefix(transform, rotate(360deg));
            }
        }
    }

    @if Feature.has-item($_animation-type, scale) {
        $scaling: map.get($animation-settings, scaling);
        transform: scale($scaling);
    }

    @if Feature.has-item($_animation-type, rotate) {
        $_setting-map: map.get($animation-settings, rotation);
        $_direction: list.nth($_setting-map, 1);
        $_rotation: list.nth($_setting-map, 2);

        @if $_direction == x {
            transform: rotateX($_rotation);
        } @else if $_direction == y {
            transform: rotateY($_rotation);
        } @else if $_direction == z {
            transform: rotateZ($_rotation);
        } @else if $_direction == default {
            transform: rotate($_rotation);
        }
    }

    @if Feature.has-item($_animation-type, translate) {
        $_setting-map: map.get($animation-settings, translate);
        $_direction: list.nth($_setting-map, 1);
        $_translation: list.nth($_setting-map, 2);

        @if $_direction == x {
            transform: translateX($_translation);
        } @else if $_direction == y {
            transform: translateY($_translation);
        } @else if $_direction == z {
            transform: translateZ($_translation);
        } @else if $_direction == default {
            transform: translate($_translation);
        }
    }
}

@function verify-setting_($query) {
    $valid: Feature.has-property($_valid-settings, $query);
    @if $valid == false {
        @error 'The setting #{$query} is not valid! Please select from the following valid settings: #{$_valid-settings}'
    }

    @return $query;
}

@function verify-type_($query) {
    $valid: Feature.has-item($_valid-animations, $query);
    @if $valid == false {
        @error 'The animation type #{$query} is not valid! Please select from the following valid animation types: #{$_valid-animations}'
    }

    @return $query;
}
