"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var NinePatch = /** @class */ (function (_super) {
    __extends(NinePatch, _super);
    function NinePatch(game, x, y, key, frame, width, height, data) {
        var _this = _super.call(this, game, x, y, key, frame) || this;
        _this.baseTexture = _this.texture.baseTexture;
        _this.baseFrame = _this.texture.frame;
        if (frame !== null && !data) {
            data = game.cache.getNinePatch(frame);
        }
        else if (!data) {
            data = game.cache.getNinePatch(key);
        }
        if (undefined === data) {
            return _this;
        }
        _this.topSize = data.top;
        _this.leftSize = data.left || _this.topSize;
        _this.rightSize = data.right || _this.leftSize;
        _this.bottomSize = data.bottom || _this.topSize;
        _this.loadTexture(new Phaser.RenderTexture(_this.game, _this.localWidth, _this.localHeight));
        _this.resize(width, height);
        return _this;
    }
    /**
     * Set the size of the container, then update all the parts
     *
     * @param width
     * @param height
     */
    NinePatch.prototype.resize = function (width, height) {
        this.localWidth = Math.round(width);
        this.localHeight = Math.round(height);
        this.renderTexture();
    };
    /**
     * Override the destroy to fix PIXI leaking CanvasBuffers
     *
     * @param args
     */
    NinePatch.prototype.destroy = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        _super.prototype.destroy.call(this, args[0]);
        this.texture.destroy(true);
        this.texture = null;
        this.baseTexture = null;
        this.baseFrame = null;
    };
    /**
     * Redraw the the current texture to adjust for the new sizes;
     */
    NinePatch.prototype.renderTexture = function () {
        // Set a new empty texture
        this.texture.resize(this.localWidth, this.localHeight, true);
        // The positions we want from the base texture
        var textureXs = [0, this.leftSize, this.baseFrame.width - this.rightSize, this.baseFrame.width];
        var textureYs = [0, this.topSize, this.baseFrame.height - this.bottomSize, this.baseFrame.height];
        // These are the positions we need the eventual texture to have
        var finalXs = [0, this.leftSize, this.localWidth - this.rightSize, this.localWidth];
        var finalYs = [0, this.topSize, this.localHeight - this.bottomSize, this.localHeight];
        this.texture.clear();
        for (var yi = 0; yi < 3; yi++) {
            for (var xi = 0; xi < 3; xi++) {
                var s = this.createTexturePart(textureXs[xi], // x
                textureYs[yi], // y
                textureXs[xi + 1] - textureXs[xi], // width
                textureYs[yi + 1] - textureYs[yi] // height
                );
                s.width = finalXs[xi + 1] - finalXs[xi];
                s.height = finalYs[yi + 1] - finalYs[yi];
                this.texture.renderXY(s, finalXs[xi], finalYs[yi]);
            }
        }
    };
    /**
     * Here we create a sprite part for the container based on the given input
     *
     * @param x
     * @param y
     * @param width
     * @param height
     * @returns {PIXI.Sprite}
     */
    NinePatch.prototype.createTexturePart = function (x, y, width, height) {
        var frame = new PIXI.Rectangle(this.baseFrame.x + this.texture.frame.x + x, this.baseFrame.y + this.texture.frame.y + y, Math.max(width, 1), Math.max(height, 1));
        return new Phaser.Sprite(this.game, 0, 0, new PIXI.Texture(this.baseTexture, frame));
    };
    return NinePatch;
}(Phaser.Sprite));
exports.NinePatch = NinePatch;
//# sourceMappingURL=NinePatch.js.map