1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.assignColorValue = assignColorValue;
7exports.getColorsAsObject = getColorsAsObject;
8exports.getObjectAsColorsXml = getObjectAsColorsXml;
9exports.getProjectColorsXMLPathAsync = getProjectColorsXMLPathAsync;
10exports.removeColorItem = removeColorItem;
11exports.setColorItem = setColorItem;
12function _Paths() {
13  const data = require("./Paths");
14  _Paths = function () {
15    return data;
16  };
17  return data;
18}
19function _Resources() {
20  const data = require("./Resources");
21  _Resources = function () {
22    return data;
23  };
24  return data;
25}
26function getProjectColorsXMLPathAsync(projectRoot, {
27  kind
28} = {}) {
29  return (0, _Paths().getResourceXMLPathAsync)(projectRoot, {
30    kind,
31    name: 'colors'
32  });
33}
34function setColorItem(itemToAdd, colorFileContentsJSON) {
35  var _colorFileContentsJSO;
36  if ((_colorFileContentsJSO = colorFileContentsJSON.resources) !== null && _colorFileContentsJSO !== void 0 && _colorFileContentsJSO.color) {
37    const colorNameExists = colorFileContentsJSON.resources.color.filter(e => e.$.name === itemToAdd.$.name)[0];
38    if (colorNameExists) {
39      colorNameExists._ = itemToAdd._;
40    } else {
41      colorFileContentsJSON.resources.color.push(itemToAdd);
42    }
43  } else {
44    if (!colorFileContentsJSON.resources || typeof colorFileContentsJSON.resources === 'string') {
45      //file was empty and JSON is `{resources : ''}`
46      colorFileContentsJSON.resources = {};
47    }
48    colorFileContentsJSON.resources.color = [itemToAdd];
49  }
50  return colorFileContentsJSON;
51}
52function removeColorItem(named, contents) {
53  var _contents$resources;
54  if ((_contents$resources = contents.resources) !== null && _contents$resources !== void 0 && _contents$resources.color) {
55    const index = contents.resources.color.findIndex(e => e.$.name === named);
56    if (index > -1) {
57      // replace the previous value
58      contents.resources.color.splice(index, 1);
59    }
60  }
61  return contents;
62}
63
64/**
65 * Set or remove value in XML based on nullish factor of the `value` property.
66 */
67function assignColorValue(xml, {
68  value,
69  name
70}) {
71  if (value) {
72    return setColorItem((0, _Resources().buildResourceItem)({
73      name,
74      value
75    }), xml);
76  }
77  return removeColorItem(name, xml);
78}
79
80/**
81 * Helper to convert a basic XML object into a simple k/v pair.
82 * `colors.xml` is a very basic XML file so this is pretty safe to do.
83 * Added for testing purposes.
84 *
85 * @param xml
86 * @returns
87 */
88function getColorsAsObject(xml) {
89  var _xml$resources;
90  if (!(xml !== null && xml !== void 0 && (_xml$resources = xml.resources) !== null && _xml$resources !== void 0 && _xml$resources.color)) {
91    return null;
92  }
93  return (0, _Resources().getResourceItemsAsObject)(xml.resources.color);
94}
95
96/**
97 * Helper to convert a basic k/v object to a colors XML object.
98 *
99 * @param xml
100 * @returns
101 */
102function getObjectAsColorsXml(obj) {
103  return {
104    resources: {
105      color: (0, _Resources().getObjectAsResourceItems)(obj)
106    }
107  };
108}
109//# sourceMappingURL=Colors.js.map