"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.resizeImageAsync = resizeImageAsync;
exports.setResizeImageFunction = setResizeImageFunction;
exports.setGetImageDimensionsFunction = setGetImageDimensionsFunction;
exports.getImageDimensionsAsync = getImageDimensionsAsync;

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _fs() {
  const data = _interopRequireDefault(require("fs"));

  _fs = function () {
    return data;
  };

  return data;
}

function _probeImageSize() {
  const data = _interopRequireDefault(require("probe-image-size"));

  _probeImageSize = function () {
    return data;
  };

  return data;
}

function _ExponentTools() {
  const data = require("../detach/ExponentTools");

  _ExponentTools = function () {
    return data;
  };

  return data;
}

function _Logger() {
  const data = _interopRequireDefault(require("../detach/Logger"));

  _Logger = function () {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * @param {string} projectDirname
 * @param {string} basename
 * @returns {} { width: number, height: number } image dimensions or null
 */
async function getImageDimensionsAsync(projectDirname, basename) {
  try {
    return await _getImageDimensionsAsync(projectDirname, basename);
  } catch (_) {}

  return null;
}

async function _getImageDimensionsWithImageProbeAsync(projectDirname, basename) {
  const imagePath = _path().default.resolve(projectDirname, basename);

  const readStream = _fs().default.createReadStream(imagePath);

  const {
    width,
    height
  } = await (0, _probeImageSize().default)(readStream);
  readStream.destroy();
  return {
    width,
    height
  };
}

let _hasWarned = false;

async function resizeImageAsync(iconSizePx, iconFilename, destinationIconPath) {
  if (process.platform !== 'darwin' && _resizeImageAsync === _resizeImageWithSipsAsync && !_hasWarned) {
    _Logger().default.warn('`sips` utility may or may not work outside of macOS');

    _hasWarned = true;
  }

  return _resizeImageAsync(iconSizePx, iconFilename, destinationIconPath);
}

async function _resizeImageWithSipsAsync(iconSizePx, iconFilename, destinationIconPath) {
  return (0, _ExponentTools().spawnAsyncThrowError)('sips', ['-Z', iconSizePx.toFixed(), iconFilename], {
    stdio: ['ignore', 'ignore', 'inherit'],
    // only stderr
    cwd: destinationIconPath
  });
} // Allow us to swap out the default implementations of image functions


let _resizeImageAsync = _resizeImageWithSipsAsync;
let _getImageDimensionsAsync = _getImageDimensionsWithImageProbeAsync; // Allow users to provide an alternate implementation for our image resize function.
// This is used internally in order to use sharp instead of sips in standalone builder.

function setResizeImageFunction(fn) {
  _resizeImageAsync = fn;
} // Allow users to provide an alternate implementation for our image dimensions function.
// This is used internally in order to use sharp instead of sips in standalone builder.


function setGetImageDimensionsFunction(fn) {
  _getImageDimensionsAsync = fn;
}
//# sourceMappingURL=../__sourcemaps__/tools/ImageUtils.js.map