1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.assignStylesValue = assignStylesValue;
7exports.getAppThemeLightNoActionBarGroup = getAppThemeLightNoActionBarGroup;
8exports.getProjectStylesXMLPathAsync = getProjectStylesXMLPathAsync;
9exports.getStyleParent = getStyleParent;
10exports.getStylesGroupAsObject = getStylesGroupAsObject;
11exports.getStylesItem = getStylesItem;
12exports.readStylesXMLAsync = readStylesXMLAsync;
13exports.removeStylesItem = removeStylesItem;
14exports.setStylesItem = setStylesItem;
15
16function _Paths() {
17  const data = require("./Paths");
18
19  _Paths = function () {
20    return data;
21  };
22
23  return data;
24}
25
26function _Resources() {
27  const data = require("./Resources");
28
29  _Resources = function () {
30    return data;
31  };
32
33  return data;
34}
35
36// Adds support for `tools:x`
37const fallbackResourceString = `<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"></resources>`;
38
39async function readStylesXMLAsync({
40  path,
41  fallback = fallbackResourceString
42}) {
43  return (0, _Resources().readResourcesXMLAsync)({
44    path,
45    fallback
46  });
47}
48
49async function getProjectStylesXMLPathAsync(projectRoot, {
50  kind
51} = {}) {
52  return (0, _Paths().getResourceXMLPathAsync)(projectRoot, {
53    kind,
54    name: 'styles'
55  });
56}
57
58function ensureDefaultStyleResourceXML(xml) {
59  var _xml, _xml$resources;
60
61  xml = (0, _Resources().ensureDefaultResourceXML)(xml);
62
63  if (!Array.isArray((_xml = xml) === null || _xml === void 0 ? void 0 : (_xml$resources = _xml.resources) === null || _xml$resources === void 0 ? void 0 : _xml$resources.style)) {
64    xml.resources.style = [];
65  }
66
67  return xml;
68}
69
70function getStyleParent(xml, group) {
71  return (0, _Resources().findResourceGroup)(xml.resources.style, group);
72}
73
74function getStylesItem({
75  name,
76  xml,
77  parent
78}) {
79  xml = ensureDefaultStyleResourceXML(xml);
80  const appTheme = getStyleParent(xml, parent);
81
82  if (!appTheme) {
83    return null;
84  }
85
86  if (appTheme.item) {
87    const existingItem = appTheme.item.filter(({
88      $: head
89    }) => head.name === name)[0]; // Don't want to 2 of the same item, so if one exists, we overwrite it
90
91    if (existingItem) {
92      return existingItem;
93    }
94  }
95
96  return null;
97}
98
99function setStylesItem({
100  item,
101  xml,
102  parent
103}) {
104  xml = ensureDefaultStyleResourceXML(xml);
105  let appTheme = getStyleParent(xml, parent);
106
107  if (!appTheme) {
108    appTheme = (0, _Resources().buildResourceGroup)(parent);
109    xml.resources.style.push(appTheme);
110  }
111
112  if (appTheme.item) {
113    const existingItem = appTheme.item.filter(({
114      $: head
115    }) => head.name === item.$.name)[0]; // Don't want to 2 of the same item, so if one exists, we overwrite it
116
117    if (existingItem) {
118      existingItem._ = item._;
119      existingItem.$ = item.$;
120    } else {
121      appTheme.item.push(item);
122    }
123  } else {
124    appTheme.item = [item];
125  }
126
127  return xml;
128}
129
130function removeStylesItem({
131  name,
132  xml,
133  parent
134}) {
135  xml = ensureDefaultStyleResourceXML(xml);
136  const appTheme = getStyleParent(xml, parent);
137
138  if (appTheme !== null && appTheme !== void 0 && appTheme.item) {
139    const index = appTheme.item.findIndex(({
140      $: head
141    }) => head.name === name);
142
143    if (index > -1) {
144      appTheme.item.splice(index, 1);
145    }
146  }
147
148  return xml;
149} // This is a very common theme so make it reusable.
150
151
152function getAppThemeLightNoActionBarGroup() {
153  return {
154    name: 'AppTheme',
155    parent: 'Theme.AppCompat.Light.NoActionBar'
156  };
157}
158
159function assignStylesValue(xml, {
160  add,
161  value,
162  targetApi,
163  name,
164  parent
165}) {
166  if (add) {
167    return setStylesItem({
168      xml,
169      parent,
170      item: (0, _Resources().buildResourceItem)({
171        name,
172        targetApi,
173        value
174      })
175    });
176  }
177
178  return removeStylesItem({
179    xml,
180    parent,
181    name
182  });
183}
184/**
185 * Helper to convert a styles.xml parent's children into a simple k/v pair.
186 * Added for testing purposes.
187 *
188 * @param xml
189 * @returns
190 */
191
192
193function getStylesGroupAsObject(xml, group) {
194  const xmlGroup = getStyleParent(xml, group);
195  return xmlGroup !== null && xmlGroup !== void 0 && xmlGroup.item ? (0, _Resources().getResourceItemsAsObject)(xmlGroup.item) : null;
196}
197//# sourceMappingURL=Styles.js.map