const lightningcssLoader = function (source, sourceMap) {
    const { implementation, ...options } = this.getOptions();
    const callback = this.async();

    if (typeof implementation !== 'function') {
        throw new TypeError(
            `[lightningcss-loader]: The "implementation" option is required and must provide "transform" function of the LightningCSS`,
        );
    }

    const { code, map } = implementation({
        filename: this.resourcePath,
        code: Buffer.from(source),
        sourceMap: this.sourceMap,
        inputSourceMap: this.sourceMap && sourceMap ? JSON.stringify(sourceMap) : undefined,
        ...options,
    });

    callback(undefined, code.toString(), map && JSON.parse(map.toString()));
};

export default lightningcssLoader;
