//
// Copyright (c) 2013. Sencha Inc.
//

$slicer-gen: map_create() !default;

$slicer-map: map_create() !default;
$slicer-map-size: 0 !default;

@function sliceit($cls, $kind, $path, $extension) {
    $url: theme-background-image($path, $extension, true);

    @if $url != none {
        $op: $kind + ':' + $url;
        $slice: add-slice($cls, $op);
    }

    @return $url;
}

@function add-slice($cls, $op) {
    $cls: quote($cls);
    $slices: map_get($slicer-map, $cls);

    @if $slices == '' {
        $slices: unquote($op);
        $slicer-map-size: $slicer-map-size + 1;
    } @else {
        $slices: append($slices, unquote($op), comma);
    }

    @return map_put($slicer-map, $cls, $slices);
}

@function slicer-background-image($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'bg', $path, $extension);
}

@function slicer-background-image-rtl($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'bg-rtl', $path, $extension);
}

@function slicer-frame-background-image($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'frame-bg', $path, $extension);
}

@function slicer-frame-background-image-rtl($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'frame-bg-rtl', $path, $extension);
}

@function slicer-corner-sprite($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'corners', $path, $extension);
}

@function slicer-corner-sprite-rtl($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'corners-rtl', $path, $extension);
}

@function slicer-sides-sprite($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'sides', $path, $extension);
}

@function slicer-sides-sprite-rtl($cls, $path, $extension: $slicer-image-extension) {
    @return sliceit($cls, 'sides-rtl', $path, $extension);
}

@function slicer-frame($cls, $frame-info) {
    @return add-slice($cls, 'frame:' + $frame-info);
}

@function slicer-background-stretch($cls, $stretch) {
    @return add-slice($cls, 'stretch:' + $stretch);
}

@mixin x-slicer($cls) {
    $cls: quote($cls);
    $slices: map_get($slicer-map, $cls);

    @if $slices != '' {
        // We don't need to use the $prefix for x-cmd-slicer and doing so would make it
        // difficult for Cmd to know what class to add...
        /*<if slicer>*/
        .x-cmd-slicer.#{$prefix}#{$cls}:before {
            display: none;
            // We need to override content when slicing:
            content: "x-slicer:#{$slices}" !important;
        }
        /*</if slicer>*/
// add a comment line to ensure the end directive gets its own line
        /* */
    }
}
