"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebGLSliceV10 = exports.WebGLSlice = void 0;
const slice_1 = require("../../../ops/slice");
const util_1 = require("../../../util");
class WebGLSlice extends slice_1.Slice {
    run(inferenceHandler, inputs) {
        return inferenceHandler.run(this, inputs);
    }
    createProgramInfo(handler, inputs) {
        return createProgramInfo(handler, inputs[0], this.starts, this.ends, this.axes);
    }
    createRunData(handler, programInfo, inputs) {
        return createRunData(handler, programInfo, inputs);
    }
}
exports.WebGLSlice = WebGLSlice;
class WebGLSliceV10 extends slice_1.SliceV10 {
    run(inferenceHandler, inputs) {
        return inferenceHandler.run(this, inputs);
    }
    createProgramInfo(handler, inputs) {
        if (!handler.session.isInitializer(inputs[1].dataId) || !handler.session.isInitializer(inputs[2].dataId) ||
            (inputs.length >= 4 && !handler.session.isInitializer(inputs[3].dataId)) ||
            (inputs.length >= 5 && !handler.session.isInitializer(inputs[4].dataId))) {
            throw new Error('dynamic slice attributes are not allowed');
        }
        if (inputs.length >= 5 && inputs[4].integerData.some((i) => i !== 1)) {
            throw new Error('currently non-1 steps is not supported for Slice');
        }
        const starts = Array.from(inputs[1].integerData);
        const ends = Array.from(inputs[2].integerData);
        const axes = inputs.length >= 4 ? Array.from(inputs[3].integerData) : [];
        return createProgramInfo(handler, inputs[0], starts, ends, axes);
    }
    createRunData(handler, programInfo, inputs) {
        return createRunData(handler, programInfo, inputs);
    }
}
exports.WebGLSliceV10 = WebGLSliceV10;
function createProgramInfo(handler, x, starts, ends, axes) {
    if (axes.length === 0) {
        axes = x.dims.slice(0).map((val, ind) => ind);
    }
    axes = util_1.ShapeUtil.normalizeAxes(axes, x.dims.length);
    starts = starts.map((start, ind) => {
        if (start > x.dims[axes[ind]] - 1) {
            return x.dims[axes[ind]];
        }
        return util_1.ShapeUtil.normalizeAxis(start, x.dims[axes[ind]]);
    });
    ends = ends.map((end, ind) => {
        if (end > x.dims[axes[ind]] - 1) {
            return x.dims[axes[ind]];
        }
        return util_1.ShapeUtil.normalizeAxis(end, x.dims[axes[ind]]);
    });
    const outputShape = x.dims.slice();
    const sliceOps = [];
    for (let i = 0; i < axes.length; i++) {
        outputShape[axes[i]] = ends[i] - starts[i];
        if (starts[i] > 0) {
            sliceOps.push(`outputIdx[${axes[i]}] += ${starts[i]};`);
        } // else { sliceOps.push(`outputIdx[${axes[i]}] += 0;`); }
    }
    const rank = outputShape.length;
    const shaderSource = `
      float process(int outputIdx[${rank}]) {
        ${sliceOps.join('\n      ')}
        return _A(outputIdx);
      }`;
    return {
        inputLayouts: [handler.getOrCreateTextureLayout(x)],
        outputLayout: handler.createTextureLayoutFromShape(outputShape),
        samplers: ['A'],
        shaderSource,
    };
}
function createRunData(handler, programInfo, inputs) {
    const inputTDs = [handler.getOrCreateTextureData(inputs[0], programInfo.inputLayouts[0])];
    return {
        inputTextureDatas: inputTDs,
        outputTextureData: handler.createTextureDataFromLayout(programInfo.outputLayout, inputTDs[0].tensor.type),
        uniformData: {}
    };
}
//# sourceMappingURL=slice.js.map