"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.yamlFileFunctions = void 0;

var _jsonStableStringify = _interopRequireDefault(require("json-stable-stringify"));

var _jsYaml = _interopRequireDefault(require("js-yaml"));

var _json = require("./json");

var _getPrettier = require("../util/get-prettier");

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

function polish(source, filePath, context) {
  // Sort properties
  const sortedJsonContent = (0, _jsonStableStringify.default)(source);
  const sortedSource = JSON.parse(sortedJsonContent);

  const sortedContent = _jsYaml.default.safeDump(sortedSource);

  const prettier = (0, _getPrettier.getPrettier)(context); // Check whether prettier can already infer the parser
  // from the current file path

  const fileInfo = prettier.getFileInfo.sync(filePath);
  let filePathForPrettier = filePath;

  if (!fileInfo.inferredParser) {
    // Add .yaml extension since prettier was not
    // able to infer the parser automatically
    filePathForPrettier = `${filePath}.yaml`;
  } // Format with prettier


  return prettier.format(sortedContent, {
    filepath: filePathForPrettier
  });
}

const yamlFileFunctions = {
  // Uses the same merge mechanism as JSON files
  merge: _json.jsonFileFunctions.merge,
  polish
};
exports.yamlFileFunctions = yamlFileFunctions;